Home Unexpeced token error when setting contentType to application/json
Reply: 0

Unexpeced token error when setting contentType to application/json

user741
1#
user741 Published in April 22, 2018, 12:41 am

I am sending data using AJAX POST. The data is JSON format. See below. However, I keep getting 'Unexpected token u in JSON at position 0'. Why is this happening? The reason I am setting contentType here is so that the Boolean field checked does not get converted to string.

var data = {
  "user": "tom",
  "number": 9,
  "checked": false
}

$.ajax({
    url: url,
    method: "POST",
    data: data,
    contentType: 'application/json',
    dataType: 'json',
    beforeSend: function (xhr) {
        xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
    },
    success: function (success) {
        console.log("success");
    }
});
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.286545 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO