• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

iOS调用Laravel接口返回错误信息

php 搞代码 4年前 (2022-01-25) 21次浏览 已收录 0个评论
文章目录[隐藏]

iOS端代码如下:

<code>AFHTTPRequestOperationManager *manage = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@"http://localhost:8000/login/signin"]];manage.requestSerializer = [AFJSONRequestSerializer serializer];manage.responseSerializer = [AFJSONResponseSerializer serializer];[manage.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];manage.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", nil];[manage POST:@"login/signin" parameters:[self.myLogin toParams] success:^(AFHTTPRequestOperation *operation, id responseObject) {        DebugLog(@"\n===========response===========\n%@:\n%@", @"login/signin", responseObject);        [_activityIndicator stopAnimating];        _loginButton.enabled = YES;    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {        DebugLog(@"\n===========response===========\n%@:\n%@", @"login/signin", error);        [_activityIndicator stopAnimating];        _loginButton.enabled = YES;    }];</code>

Laravel的代码如下:

<code>routes.php----------Route::post('login/signin',[    'as'=>'signin','uses'=>'Login\LoginController@signin']);LoginController.php----------public function signin(){      $message = "登录成功";    $data = array('phone'=>'13800138000','password'=>'123456');    $code = 0;    return response()->json(['message'=>$message,'data'=>$data,'code'=>0]);}</code>

提示错误:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7fe00d90a3a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

但是将Route::post 改成 Route::get

manage POST:@”login/signin” 改成 manage GET:@”login/signin”

可以返回数据如下:

<code>{    code = 0;    data =     {        password = 123456;        phone = 13800138000;    };    message = "\U767b\U5f55\U6210\U529f";}</code>

请问问题出在哪里,求帮助,谢谢各位:D 。

使用Paw查看POST数据会出现php错误如下:

<code>Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid thiswarning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0Warning: Cannot modify header information - headers already sent in Unknown on line 0</code>

回复内容:

iOS端代码如下:

<code>AFHTTPRequestOperationManager *manage = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@"http://localhost:8000/login/signin"]];manage.requestSerializer = [AFJSONRequestSerializer serializer];manage.responseSerializer = [AFJSONResponseSerializer serializer];[manage.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];manage.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", nil];[manage POS<strong>(本文来源gaodai#ma#com搞@@代~&码网</strong><pre>搞代gaodaima码

T:@”login/signin” parameters:[self.myLogin toParams] success:^(AFHTTPRequestOperation *operation, id responseObject) { DebugLog(@”\n===========response===========\n%@:\n%@”, @”login/signin”, responseObject); [_activityIndicator stopAnimating]; _loginButton.enabled = YES; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { DebugLog(@”\n===========response===========\n%@:\n%@”, @”login/signin”, error); [_activityIndicator stopAnimating]; _loginButton.enabled = YES; }];

Laravel的代码如下:

<code>routes.php----------Route::post('login/signin',[    'as'=>'signin','uses'=>'Login\LoginController@signin']);LoginController.php----------public function signin(){      $message = "登录成功";    $data = array('phone'=>'13800138000','password'=>'123456');    $code = 0;    return response()->json(['message'=>$message,'data'=>$data,'code'=>0]);}</code>

提示错误:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7fe00d90a3a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

但是将Route::post 改成 Route::get

manage POST:@”login/signin” 改成 manage GET:@”login/signin”

可以返回数据如下:

<code>{    code = 0;    data =     {        password = 123456;        phone = 13800138000;    };    message = "\U767b\U5f55\U6210\U529f";}</code>

请问问题出在哪里,求帮助,谢谢各位:D 。

使用Paw查看POST数据会出现php错误如下:

<code>Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid thiswarning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0Warning: Cannot modify header information - headers already sent in Unknown on line 0</code>

phpinfo() 知道 always_populate_raw_post_data 修改成为-1即可


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:iOS调用Laravel接口返回错误信息

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址