Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
101
rated 0 times [  106] [ 5]  / answers: 1 / hits: 33381  / 8 Years ago, mon, november 21, 2016, 12:00:00

My knowledge of ajax and JSON is limited, but I know that using JSON.stringify in an ajax call can sometimes be useful. I have an ajax call below that works fine, while the one below it with the stringify method that does not work. I am wondering if I am using .stringify correctly, and if not, when should I use JSON.stringify in ajax, if ever. I am using MVS with a model, view, and controller.



This is how I usually do ajax calls, and how i build the url portion.



    function AddEquipment(id, name, type, description, email) {
$.ajax({
url: '@Url.Action(AddEquipment, Home)' + '/?id=' + id +
&name= + name + &type= + type + &description= +
description + &email= + email,
type: GET,
cache: false,
datatype: JSON,
success: function(result) {
//do stuff
}
});
}


Below I have tried using JSON.stringify instead of building the entire url manually, and it does not work.



    function AddEquipment(id, name, type, description, email) {
$.ajax({
url: '@Url.Action(AddEquipment, Home)',
type: GET,
cache: false,
datatype: JSON,
data: JSON.stringify({
id: id,
name: name,
type: type,
description: description,
email: email
}),
success: function(result) {
//do stuff
}
});
}


the controller method this goes with accepts id as an int, while everything else is a string. I have used JSON.stringify before with mixed variables (ints, bools, strings) without an issue.



Any helpful information is greatly appreciated,



Thanks!


More From » jquery

 Answers
33

These are two different strings (values that they eventually evaluate to be). One is not equal to other. Stringify will not yield you ' = ' as you require.



Read this post to pass data on a get call



JSON.stringify({
id: id,
name: name,
type: type,
description: description,
email: email
}),

url: '@Url.Action(AddEquipment, Home)' + '/?id=' + id +
&name= + name + &type= + type + &description= +
description + &email= + email

[#59974] Friday, November 18, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
miranda

Total Points: 655
Total Questions: 110
Total Answers: 121

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
miranda questions
Sun, Jun 6, 21, 00:00, 3 Years ago
Tue, Mar 16, 21, 00:00, 3 Years ago
Sun, Feb 7, 21, 00:00, 3 Years ago
Mon, Jan 18, 21, 00:00, 3 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
;