首页 > 后端开发 > php教程 > iOS调用Laravel接口返回错误信息

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

WBOY
发布: 2016-06-06 20:32:39
原创
1398 人浏览过

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 this
warning set 'always_populate_raw_post_data' to '-1' in php.ini and 
use the php://input stream instead. in Unknown on line 0

Warning: 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 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 this
warning set 'always_populate_raw_post_data' to '-1' in php.ini and 
use the php://input stream instead. in Unknown on line 0

Warning: Cannot modify header information 
- headers already sent in Unknown on line 0
</code>
登录后复制
登录后复制

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

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板