Laravel Json Stringify responseText如何处理它?

时间:2017-05-22 09:51:58

标签: javascript json ajax laravel

我的Laravel代码如下所示

public function validateRegisterToBidForm(Request $request){

    $validationArray = $this->getValidationArray();

    $this->validate($request, $validationArray);
    return response()->json();

}//validateRegisterToBidForm

在Ajax中收到上面的输出 如果错误,那么我将输出字符串化为上面并获得跟随json

{"readyState":4,"responseText":"{\"fname\":[\"This field is missing or incorrect.\"],\"lname\":[\"This field is missing or incorrect.\"],\"email\":[\"This field is missing or incorrect\"],\"password\":[\"This field is missing or incorrect.\"],\"adr1\":[\"This field is missing or incorrect.\"],\"city\":[\"This field is missing or incorrect.\"],\"state\":[\"This field is missing or incorrect.\"],\"phone\":[\"This field is missing or incorrect.\"],\"primary_phone\":[\"This field is missing or incorrect.\"],\"pass_dri_license\":[\"This field is missing or incorrect.\"],\"card_type\":[\"This field is missing or incorrect.\"],\"cardno\":[\"This field is missing or incorrect\"],\"cvv\":[\"This field is missing or incorrect.\"],\"fname_b\":[\"This field is missing or incorrect.\"],\"lname_b\":[\"This field is missing or incorrect.\"],\"email_b\":[\"This field is missing or incorrect.\"],\"adr1_b\":[\"This field is missing or incorrect.\"],\"city_b\":[\"This field is missing or incorrect.\"],\"state_b\":[\"This field is missing or incorrect.\"],\"phone_b\":[\"This field is missing or incorrect.\"],\"primary_phone_b\":[\"This field is missing or incorrect.\"]}","status":422,"statusText":"Unprocessable Entity"}

如何提取价值?什么是正确的方法?

1 个答案:

答案 0 :(得分:0)

您可以进行此类通话:

 $.ajax({
        type: "POST",
        url: "the/url",
        data: {the: data},
        success: function (myresponse) {
            console.log(myresponse);
        },
        error: function(xhr, status, error) {
            var err = JSON.parse(xhr.responseText);
            console.log(err);
        }
    });

实际上,这就是我使用的内容:即JSON.parse();,因此您可以使用err变量选择每个字段,例如err.fname

我希望这会有所帮助:)