admin管理员组文章数量:1300023
I'm trying to post some data to a webservice and utilize the XML data it returns. I saw the example in JQuery's documentation for the post() function:
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
process(data);
},
"xml"
);
What does the process function do? What does the data look like? Do I assign this processed data to a variable? Just trying to get an idea of how I would utilize the values returned from this post().
I'm trying to post some data to a webservice and utilize the XML data it returns. I saw the example in JQuery's documentation for the post() function:
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
process(data);
},
"xml"
);
What does the process function do? What does the data look like? Do I assign this processed data to a variable? Just trying to get an idea of how I would utilize the values returned from this post().
Share Improve this question asked Sep 1, 2011 at 18:56 mheaversmheavers 30.2k62 gold badges200 silver badges326 bronze badges2 Answers
Reset to default 5What does the process function do?
There is no such standard function. It's up to you to define it, so it will do whatever you tell it to do.
What does the data look like?
That will depend on what does your web service returns. If it is XML, it will represent a XML tree. So for example if you service returns the following xml:
<foo>
<bar>some value</bar>
</foo>
you could query the value of the bar
node like this:
function(data) {
var value = $('bar', data).text();
alert(value);
}
And here's a live demo where you can see it in action.
process(data);
can be any function that you want to call that does something with the data
. In most cases, you'd want to assign the data to an element or do some other massaging before displaying to the user.
Unless its very involved, you can skip calling a separate function too, e.g.:
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
$('#target').html(data); // assuming data is a html string
},
"xml"
);
Taking it to process shape:
$.post("test.php", { name: "John", time: "2pm" },
function(data) {
process(data)
},
"xml"
);
function process(data) {
$('#target').html(data); // assuming data is a html string
}
If your data in some other format, then you'd massage it to extract relevant bits or transform it in some way inside process
or any custom function.
E.g., let's say you get a JSON object back:
data = { "status": "success", "text": "Processed Succesfully" };
then in your callback, you'd check for status and the display message appropriately
function process(data) {
if(data.status == "success")
$('#target').html(data.text); // assuming data is a html string
else
alert("Error");
}
本文标签: javascriptjqueryunderstanding jquery39s post() and javascript39s process() functionsStack Overflow
版权声明:本文标题:javascriptjquery - understanding jquery's post() and javascript's process() functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741655549a2390737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论