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

iOS: JSON pars error when using return instead of echo in PHP service file

我有以下 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 错误?

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

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