I have this where edit_FName & edit_LName represent asp.net textboxes:
$.ajax({
type: "POST",
url: document.URL + "/EditName",
data: "{fN开发者_开发百科ame:'" + edit_FName.val() + "'" +
",lName:'" + edit_LName.val() + "'}",
And I need it to allow for both single and double quotes in the names. I Kinda figure I need to use stringify but I cannot get it to behave the way I want. I tried:
$.ajax({
type: "POST",
url: document.URL + "/EditName",
data: "{fName:'" + JSON.stringify(edit_FName.val()) + "'" +
",lName:'" + JSON.stringify(edit_LName.val()) + "'}",
etc...
but that doesn't work at all so I guess I don't have an understanding of how I'm supposed to use stringify to put together the data.
I think what you want here is
...
data: JSON.stringify({fName:edit_FName.val(),
lName:edit_LName.val()}),
...
One problem is you need quotes around fName, for example also.
$.ajax({
type: "POST",
url: document.URL + "/EditName",
data: "{'fName':'" + edit_FName.val() + "'" +
",'lName':'" + edit_LName.val() + "'}",
How doesn't it work? From looking at your code, my first guess would a syntax error. I generally solve/figure out these problems by playing around with a REPL. Try Firebug.
JSON.stringify looks like it would work if you have the JSON library installed.
I think you could use the dataType:"JSON" option of the ajax call to acheive the same result, your resulting code would look like this.
$.ajax({
type: "POST",
url: document.URL + "/EditName",
dataType:"json",
data: {fName:edit_FName.val(),
lName:edit_LName.val()}});
Manually escaping or constructing json data is a bad idea, even though this is being sent back to the server, where it will be handled differently. If you were constructing this serverside and sending it to the client, this is a great opportunity for script injection
精彩评论