duanbarong4321 2018-01-22 17:03
浏览 71
已采纳

Laravel Ajax响应返回json上方的文本

I'm writing to my model using ajax and if there is an error like a duplicate I need it to throw a json response.

$model = Product::find($id);
$model->{$col} = $request->value;    

try{
        $model->save();
    } catch (Exception $e){   
        return Response::json(['error' => $e->getMessage()], 500);
    }

If there is an error it returns the following with a response code of 200 so my error handling in ajax doesn't get to touch this. I also can't handle it in the success function because it's not pure json

HTTP/1.0 500 Internal Server Error
Cache-Control: no-cache, private
Content-Type:  application/json
Date:          Mon, 22 Jan 2018 16:06:17 GMT

{"error":"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry...."}

I found adding http_response_code(500) Before the return did then give me a status response of 500 but the responseText was still the same and contained the extra text and not just pure json

The response I want should look this

{"error":"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry...."}

Here's my js ajax code

var post_data = {id:$(this.attr('id),value:$(this).value()}   



$.ajax({
    type:'POST',
    data: post_data,
    beforeSend:function(){
       //stuff here to show user somethign is happening
    },
    success:function(returned_data){

        console.log('successful!')

        console.log(returned_data);
    },
    error:function(event, jqXHR, ajaxSettings, thrownError ) {
         console.log('event');
         console.log(event);
         console.log('jqXHR');
         console.log(jqXHR.responseText);

         console.log('thrownError');
         console.log(thrownError);
    }

}

What am I doing wrong that laravel is sending this text that looks like the headers, instead of just the pure json?

Edit: Added code before try catch

Also, I am purposefully causing this error so I can make sure my error handling is working but isn't because I'm not getting pure json and it's returning a 200 response code

  • 写回答

2条回答 默认 最新

  • duannan3959 2018-01-24 14:21
    关注

    Ok so after lots of testing I found the problem. With my ajax data that I send back to the server, I send a function name with that calls the function to use. The problem was that I was using echo for pushing the data back.

    echo $_POST['ajax_function_name']();
    

    Changing the echo to return will now return the correct json response when using Response::json();

    The reason behind this happening was moving functions from a legacy app to the new laravel app and overlooked this issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题