我对HTTP
提出了AFNetworking
次请求:
NSMutableDictionary *dictionary = [NSMutableDictionary new];
[dictionary setObject:@"login" forKey:@"request"];
[dictionary setObject:@"q" forKey:@"userName"];
[dictionary setObject:@"q" forKey:@"password"];
DDLogDebug(@"LoginView - login - Dictionary: %@", [dictionary description]);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
//[manager.securityPolicy setAllowInvalidCertificates:true];
AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL
parameters:dictionary
success:^(AFHTTPRequestOperation *operation, id responseObject) {
dispatch_async(dispatch_get_main_queue(), ^{
DDLogDebug(@"LoginView - Success Response: %@", responseObject);
NSString *success = [responseObject objectForKey:@"success"];
if ([success isEqualToString:@"1"]) {
DDLogDebug(@"LoginView - Login successful!");
[self performSegueWithIdentifier:kSegueLoginToTimeLine sender:self];
}
else {
DDLogError(@"LoginView - Login failed!");
[HMXCommonMethods showMessage:@"Kullanıcı adı ya da şifre hatalı" withTitle:@"Hata!"];
}
});
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
DDLogError(@"LoginView - Error Response: %@", [error localizedDescription]);
NSInteger statusCode = [operation.response statusCode];
switch (statusCode) {
case 500:
[HMXCommonMethods showMessage:@"Sunucu hatası nedeniyle giriş yapılamadı!" withTitle:@"Hata!"];
break;
case 408:
[HMXCommonMethods showMessage:@"İstek zaman aşımına uğradı!" withTitle:@"Hata!"];
break;
case 404:
[HMXCommonMethods showMessage:@"Sunucuya erişilemedi!" withTitle:@"Hata!"];
break;
default:
[HMXCommonMethods showMessage:@"Sunucu hatası nedeniyle giriş yapılamadı!" withTitle:@"Hata!"];
break;
}
});
}];
[operation start];
DDLogError(@"LoginView - login - request started.");
当我在PHP
文件中使用以下echo
代码回复此请求时,它可以正常工作:
$response = "{\"success\":\"1\",";
$response = $response . "\"id\":\"" . $id . "\",";
$response = $response . "\"companyid\":\"" . $cid . "\",";
$response = $response . "\"companyname\":\"" . $cname . "\",";
$response = $response . "\"name\":\"" . $name . "\",";
$response = $response . "\"surname\":\"" . $surname . "\",";
$response = $response . "\"email\":\"" . $email2 . "\",";
$response = $response . "\"password\":\"" . $password2 . "\"}";
$stmt2->close();
$log = "Company ID = " . $cid . "\n" .
"Company Name = " . $cname . "\n" .
"Company Address = " . $caddress . "\n" .
"Company Phone = " . $cphone . "\n" .
"Company Web Page = " . $cwebpage . "\n";
$this->logger->debug("GetUser: Company Info:\n" . $log);
$this->logger->debug("GetUser: Response:\n" . $response);
echo $response;
如果我使用echo
更改return
,我会JSON pars error 3840
。我也试过
echo json_encode($response)
return json_encode($response)
但它没有用,我得到了同样的错误。如何在没有return
错误的情况下使用JSON
?
答案 0 :(得分:3)
为了让AJAX能够从PHP读取输出,PHP必须回应响应。如果使用return
AJAX无法读取和使用数据,因为它希望接收以下几种类似文本的数据类型之一:XML,HTML,脚本,JSON,JSONP或纯文本。 return
没有提供。
此外,您不应该手工形成JSON字符串,因为它可能是一个复杂的事情,可能会出现许多语法错误。利用PHP的JSON函数 - json_encode()
和json_decode()
。