Posting user entered text using $.ajax, how to pass text content in the data property?
What options do I have for passing large text in a call to $.ajax(...)
?
I have the content stored in a va开发者_开发技巧riable already:
articleText
So I have:
$.ajax(
type: "POST",
url: "/test/add_article",
dataType: "json",
data: ??????,
success: function(d) {
alert(d);
}
});
In the docs they show:
"p1=asdfasdf&p2=2sdfasdf"
Also:
data: ({someName: someValue })
I like the latter, so is the someValue
where I put my variable?
What about encoding it or is it just like a form post where I can handle that on the server-side?
)
You would simply do data: { aT: articleText }
. Then in your server-side script you can access that text as post variable aT
...in PHP it would be: $_POST['aT']
.
jQuery converts { aT: articleText }
to "aT=myTextContentWouldBeHere"
.
Just change data
to
data: {"someName": someValue}
if someValue is a variable, else
data: {"someName": "someValue"}
if someValue itself is the value.
精彩评论