sending a post from iOS app to php script not working… Simple solution is likey

后端 未结 3 1098
名媛妹妹
名媛妹妹 2021-01-01 01:16

I have done this several times before but for some reason I can\'t get the post to go through... I tried the php script with the variables set to _POST and without... When t

相关标签:
3条回答
  • 2021-01-01 02:08

    As I am a iPhone developer, I can say that your Objective-C code is correct. Also ensure that the data you are sending is not empty by

    NSLog(@"%d,[postData length]");
    

    it should not print 0

    0 讨论(0)
  • 2021-01-01 02:15

    Use following code. make sure that post string's receipt is a key and will be used in server. client side code

     NSString *receipt1 = @"username";
    
        NSString *post =[NSString stringWithFormat:@"receipt=%@",receipt1];
       NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:@"http://localhost:8888/validateaction.php"]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
    
        NSHTTPURLResponse* urlResponse = nil;
        NSError *error = [[NSError alloc] init];
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
        NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"Response Code: %d", [urlResponse statusCode]);
    
        if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
        {
            NSLog(@"Response: %@", result);
        }
    
    }
    

    server side php script.

    validation.php

    <?php
     if(_POST)
        {
            if($_POST['receipt'] == 'username')
            {
                echo "post successfull";
             $receipt   = $_POST['key1'];
             echo $receipt;
            }
            else
        {
            echo "not post";
        }
    ?>
    
    0 讨论(0)
  • 2021-01-01 02:17

    $_POST is an array, not a function. You need square brackets to access array indices:

    $newShift = $_POST['shift'];
    $bartenderUsername = $_POST['username'];
    
    0 讨论(0)
提交回复
热议问题