Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax json adding trailing colon

var mydata = {
    "one": {
        "brand": "acer",
        "cost": "212132"
    }
}


$.ajax({
        url: 'http://localhost',
        type: 'post',
        dataType: 'json',
        data: JSON.stringify(mydata)
    });

Above code is adding a colon to the data when i view the form send data in chrome dev tools. Is there anything wrong with the request?

like image 267
Rafa Tost Avatar asked Aug 31 '25 01:08

Rafa Tost


2 Answers

You are viewing application/x-www-form-urlencoded parsed data. So Chrome is trying to establish key value pairs. Click "view source" near the "Form Data" heading in the network tab view. You will see your JSON without the colon.

like image 148
Castyr Avatar answered Sep 02 '25 14:09

Castyr


Was the same for me. I did not see the colon when viewing source. BUT it did not work. My solution was to add the missing contentType

$.ajax({
        url: `url`,
        contentType: "application/json",
        type: "POST",
        data: JSON.stringify(data)
  });
like image 27
kevinSpaceyIsKeyserSöze Avatar answered Sep 02 '25 15:09

kevinSpaceyIsKeyserSöze