2016-04-28 167 views
0

我對iOS很新。我正試圖通過post方法將數據發送到PHP。在PHP中,它不能採用像$_POST['data']這樣的數據,但它需要$_GET['data']。我的iOS代碼如下。IOS發送post方法數據可以在php中作爲get方法訪問

NSString *strURL = [NSString stringWithFormat:@"http://example.com/app_respond_to_job?emp_id=%@&job_code=%@&status=Worker-Accepted&comment=%@",SaveID2,txtJobcode1,alertTextField.text]; 

NSURL *apiURL = [NSURL URLWithString:strURL]; 
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:apiURL]; 

[urlRequest setHTTPMethod:@"POST"]; 

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 

_receivedData = [[NSMutableData alloc] init]; 

[connection start]; 
NSLog(@"URL---%@",strURL); 

有人可以解釋爲什麼,這將是非常有益的。

回答

3

請下載此文件https://www.dropbox.com/s/tggf5rru7l3n53m/AFNetworking.zip?dl=0

而在你的項目中導入文件 在#進口定義「AFHTTPRequestOperationManager.h」

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"Your Url"]]; 
    NSDictionary *parameters = @{@"emp_id":SaveID2,@"job_code":txtJobcode1.text,@"status":alertTextField.text}; 

    AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 

    } success:^(AFHTTPRequestOperation *operation, id responseObject) { 


     NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject); 
     manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
     [responseObject valueForKey: @"data"]; 



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

     NSLog(@"Error: %@ ***** %@", operation.responseString, error); 
    }]; 
    [op start]; 
0

,因爲你在URL
我想通過查詢字符串發送數據它會工作,如果你嘗試在請求的正文中傳遞數據:[urlRequest setHTTPBody:...]

0

POST參數來自請求體,而不是f rom網址字符串。你需要:

  • 呼叫setHTTPBody的請求,並提供URL編碼的字符串(沒有問號,IIRC)作爲體數據
  • 呼叫setValue:forHTTPHeaderField:的內容類型設置爲application/x-www-form-urlencoded
  • 要麼刪除對[connection start]的呼叫,要麼使用initWithRequest:delegate:startImmediately:,這樣您就不會兩次啓動連接。

最後一個很重要。如果嘗試兩次啓動連接,可能會得到奇怪的結果。 :-)