douzhangkui2467 2016-10-04 00:32
浏览 82
已采纳

Laravel使用额外的''`quote:“'{'status”:500,“m返回JSON响应

I'm using jQuery $.ajax to send ajax requests to my Laravel 5.1 API.

I'm trying to simply output the error response from the server, but am unable to JSON.parse the response due to a rogue ' at the beginning of the responseText property value.

Why is that there?

enter image description here

Frontend:

API.Auth.register(params).done(function (result) {

    // Do stuff

}).error(function (xhr, status, error) {
    console.log('Registration error: ', xhr);
    var parsed_response = JSON.parse(xhr.responseText);
    console.log('parsed_response', parsed_response);
    alertify.error('There was a problem with your registration.');
});

register : function (params)
{
    return $.ajax({
        url: globals.env.api_host + globals.env.api_ver + '/register',
        type: 'POST',
        data: params,
        dataType: 'json',
        cache: true
    })
},

Laravel 5.1: I've tried wrapping the properties in " ", with the same error result:

public function register(Request $request)
{
    try {
        $new_user = $this->create($request->all());
    } catch (QueryException $e) {
        // error code 23000 is unique SQL unique constraint error - TODO: abstract this checking
        if ($e->getCode() === '23000') {
            return response()->json(['status' => 500, 'message' => 'This e-mail already exists. Try a new e-mail or logging in.']);
        } else {
            return response()->json(['status' => 500, 'message' => 'There was an error processing registration.']);
        }
    }

EDIT: I tested if the ' persists if I just return a string:

    //Original Route:
    //$router->post('/register', 'Auth\AuthController@register');

    $router->post('/register', function () {
        return 'hello world';
    });

Response: 'hello world

That single quote at the beginning persists.


Side note: I can't find anything on capturing a custom Exception message in jQuery ajax. My preferred way to return errors is custom exceptions:

throw new BadRequestHttpException('There was an error processing registration.');

but $.ajax offers no way to capture the exception message, so I've resorted to returning response()->json... from Laravel.

Any thoughts on this?


EDIT: Found it. Someone made an accidental change to the config/app.php file and added a '. Odd how Laravel treats this though by still serving the response, but prepends the '.

enter image description here

  • 写回答

1条回答 默认 最新

  • douan3414 2016-10-04 01:17
    关注

    After doing a lot of searching, @Derek suggested I check source control for rogue additions such as ' single quote.

    I found someone had accidentally committed a ' to the config/app.php file.

    enter image description here

    PHP treats anything outside the <php> tags as output, hence why it was being "prepended" to any response.

    This was breaking JSON.parsing on the frontend obviously because the response payload was malformed JSON.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型