admin管理员组

文章数量:1323723

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?

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?

Share Improve this question asked Nov 11, 2013 at 5:42 Rafa TostRafa Tost 3995 silver badges13 bronze badges 7
  • you didn't close the quote in url: 'http://localhost,. It should be url: 'http://localhost', – RRikesh Commented Nov 11, 2013 at 5:45
  • you missed single quote after url – Dart Commented Nov 11, 2013 at 5:45
  • 2 if you want to send the data as parameters then data: mydata – Arun P Johny Commented Nov 11, 2013 at 5:49
  • 2 Do you mean that you see a trailing : in Network > Headers > Form Data ({"one":{"brand":"acer","cost":"212132"}}:) if so thats what I also see. It is because you send it as plain data - passing it with data: JSON.stringify(mydata) - and not with data: mydata, but it does not mean that the colon is really send to the server. – t.niese Commented Nov 11, 2013 at 5:54
  • 2 could be, cause no identifier is send(i mean variable name, like login="some login"); you can try {data: JSON.stringify(mydata)} – Dart Commented Nov 11, 2013 at 6:01
 |  Show 2 more ments

2 Answers 2

Reset to default 8

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.

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)
  });

本文标签: javascriptajax json adding trailing colonStack Overflow