Rohullah Rajaee Rad Rohullah Rajaee Rad - 1 month ago 18
Ajax Question

Json Error In Laravel Ajax

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

Answer

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.