我正在使用POST方法向我們的服務器發送JSON字符串。應該發生的是它應該返回一個顯示「Array(JSON String)Array(JSON String)」的響應。響應包含兩個數組:第一個數組填充如果我使用POST方法,而第二個數組填充如果我使用GET方法。我嘗試通過GET方法發送JSON,實際上,第二個數組已填充。但是,當我嘗試通過POST方法發送JSON時,兩個數組都返回爲空。通過POST發送JSON不起作用 - 目標C
這裏是我使用的代碼:
NSData *requestData = [NSJSONSerialization dataWithJSONObject:sqlArray options:NSJSONWritingPrettyPrinted error:&error];
NSURL *url = [NSURL URLWithString:@"http://myurl/xmlrpc/imwebsrvcjson.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:requestData];
NSData *result =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *returnString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
if (error == nil){
NSLog(@"The Result string: %@", returnString);
}
你能告訴我什麼是錯我的代碼?
另一端看到什麼? –
@熱舔我真的不知道,因爲可以訪問服務器的人不在身邊。他只是告訴我,爲了讓我知道POST是否成功,我會收到上面的迴應。 :/ – user1412469
這類似於 http://stackoverflow.com/questions/4456966/how-to-send-json-data-in-the-http-request-using-nsurlrequest –