user3808241
user3808241

Reputation: 13

Cannot send json to server, an ajax error occurs

I tried this code on my site, then on other site, then on jquery.com via console:

jQuery.ajax({
url: '/',
type: 'POST',
data: '{"test":"test"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json'
});

$(document).ajaxError(function(e, jqXHR, ajaxSettings, thrownError){
    console.log(thrownError);
});

So the console logs this:

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

I don't understand why. It looks that everything is correct in my code.

Upvotes: 1

Views: 59

Answers (2)

bMalum
bMalum

Reputation: 398

I think you have a typo in Line 4

Try:

data: {test:"test"},

And it should work - I hope :)

Sorry, i had the 3 Minutes 1 Post Problem here and wasn't able to see the comment from Victory

Upvotes: 1

Igor
Igor

Reputation: 33963

You are passing a string literal rather than an object:

data: { test: 'test' },

Upvotes: 3

Related Questions