0

I'm Use Laravel 5.2 And Ajax For Crud Insert to Database is Correct But When Laravel Response The Browser Show Below Error In Console

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Laravel Code:

return response()->json('ok'); 

Jquery Code:

    $.ajax({             
type: type,             
url: my_url,             
data: formData,             
dataType: 'json',             
success: function (data) {
....
error: function(data){// Error...                 
var errors = $.parseJSON(data.responseText);                 console.log(errors);                 
$.each(errors, function(index, value) {                     $.gritter.add({                         
title: 'Error',                         
text: value                     
});                 
});             
}

RedyState=4 Staus = 200

3 Answers 3

2

Can you paste your all controller code related to this error? Make sure you're not echoing anything before returning json response.

For example,

Route::get('/', function () {
    echo "hi";
    return response()->json('ok'); 
});

It would cause parse error.

Sign up to request clarification or add additional context in comments.

1 Comment

YES!! Solved...Before respnce() I Use echo()
0

With the Json response, what if you try something like.

return response()->json(['status'=>'ok']); 

1 Comment

Ok so what does your output look like. Can you post that in your question.
0

You javascript code should be

$.ajax({
    type: type,
    url: my_url,
    data: formData,
    dataType: 'json',
    success:function (xhr){
        var data = xhr.data;
    },
    error: function (error){

    }
});

And why are you using var errors = $.parseJSON(data.responseText); ? Your response will be always json, no?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.