9

JSON stands for javascript object notation (as I'm sure you're aware), so why, when sending json via ajax do you need to turn it into a string to send it? Is it simply a formatting thing, or what?

This may belong in another place, if so, let me know, I'll close it and move it.

Obviously, I'm not looking for opinions, I'd like to know the actual answer.

Just to make sure I'm clear, I understand what JSON.stringify() does, and its counterpart JSON.parse(). I just want to know, why using stringify is required.

Thanks!

3
  • 5
    The reason is: HTTP only knows plain text! Ajax itself is agnostic for special data types, it can be used to send and receive arbitrary data types, which are transmitted as plain text data in the HTTP body. So if JSON is wanted, the text from the HTTP level has to be converted from/to the JSON data format somewhere.
    – rplantiko
    Commented Sep 14, 2015 at 17:33
  • What exactly would it mean to send a raw JavaScript object in an HTTP request?
    – Pointy
    Commented Sep 14, 2015 at 17:34
  • JSON is a string representation of data, similar to XML. Stringify converts JavaScript objects (and arrays) into this string format.
    – gen_Eric
    Commented Sep 14, 2015 at 17:35

3 Answers 3

16

when sending json via ajax do you need to turn it into a string to send it?

If it isn't a string, then it isn't JSON in the first place.

JSON is a text based data format. HTTP is a text based communications protocol.

JSON stands for javascript object notation

JSON is based on the syntax for JavaScript literals. A JavaScript object is not JSON.

2
  • So, while JSON looks like an object literal from JavaScript, it's its own data type, and has to be a string. That's interesting. Appreciate the answer. I'll mark this as correct once it lets me.
    – Jacques
    Commented Sep 14, 2015 at 17:43
  • 1
    What do you mean exactly? Commented Dec 30, 2016 at 21:18
3

AJAX is all about HTTP requests, which are basically "text" requests to a server. That's the main reason why you have to stringify your object: That way it's turned into text that can "travel" over HTTP.

0

When sending data to a web server, the data has to be a string.

That's why we are using JSON.stringify() function to convert data to string and send it via XHR request to the server.

        // Creating a XHR object 
        let xhr = new XMLHttpRequest();
        let url = "submit.php"; 

        // open a connection 
        xhr.open("POST", url, true); 

        // Set the request header i.e. which type of content you are sending 
        xhr.setRequestHeader("Content-Type", "application/json"); 

        // Converting JSON data to string 
        var data = JSON.stringify({ "name": name.value, "email": email.value }); 

        // Sending data with the request 
        xhr.send(data);  
3
  • 1
    I appreciate your answer, but this is a really old question, and your answer doesn't actually answer the question, it simply restates the question in the answer. It brings no value to the question or other answers. It looks like you're new to stack, so I'll say this: Unless your answer brings something new or valuable to a question, don't post it. Specifically if the question is several years old with a (still valid) accepted answer.
    – Jacques
    Commented Jan 22, 2020 at 7:09
  • You are right. I am new here and I thought that examples would help some other people as well like it did to me. And thanks for the advice though. Commented Jan 25, 2020 at 17:40
  • I agree that examples are helpful, however the example should help answer the question. The example used shows how to send an Ajax request, but the question didn’t ask how to send one, it’s asked why. If this would have been posted when the question was first asked, it probably would have received downvotes.
    – Jacques
    Commented Jan 25, 2020 at 23:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.