gpt4 book ai didi

php - iOS:在 PHP 服务文件中使用 return 而不是 echo 时出现 JSON 解析错误

转载 作者:行者123 更新时间:2023-11-28 19:52:46 24 4
gpt4 key购买 nike

我有以下 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)

但它没有用,我得到了同样的错误。如何在没有 JSON 错误的情况下使用 return

最佳答案

为了让 AJAX 能够从 PHP 读取输出,PHP 必须回应响应。如果您使用 return,AJAX 将无法读取和使用数据,因为它希望接收以下几种类似文本的数据类型之一:XML、HTML、脚本、JSON、JSONP 或纯文本。 return 不提供这一点。

此外,您不应手动形成 JSON 字符串,因为这可能是一件复杂的事情,并且可能会出现许多语法错误。使用 PHP 的 JSON 函数 - json_encode()json_decode() .

关于php - iOS:在 PHP 服务文件中使用 return 而不是 echo 时出现 JSON 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28194094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com