0

I need to send data into controller. So I have to use ajax call to send it.

$.ajax({
       url: self.options.link,
       data: {product_id: product, form_key: formKey, email: emailId, mobile: mobNo}
});

Why and when we use the data:JSON.stringify({}); and data:{} for sending post data via ajax.

$.ajax({
       url: self.options.link,
       data: JSON.stringify({product_id: product, form_key: formKey, email: emailId, mobile: mobNo})
});

Thanks.

1 Answer 1

1

A common use of JSON is to exchange data to/from a web server.

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

Convert a JavaScript object into a string with JSON.stringify().

source: https://www.w3schools.com/js/js_json_stringify.asp

data: JSON.stringify() serializes a JS object into a JSON string, on the other hand, it’s only actually JSON when it’s used in a string context.

data:{} here you sending the json object directly not the string.

Please also check : https://stackoverflow.com/questions/13981255/why-is-it-necessary-to-call-json-stringify-when-submitting-object-data-to-server

2
  • In which place is suitable for sending data with JSON.stringify() and without JSON.stringify().
    – Mahi M
    May 21, 2019 at 10:58
  • I have updated my asnwer, let me know if any further query
    – Saphal Jha
    May 21, 2019 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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