ajax request with javascript prototype
How should I do to attach directl开发者_运维知识库y the params via post method with ajax.updater defined in prototype library?
The implicit method used by prototype is POST
and you got two possibilities to submit post data. Either via the parameters
option, or via the postBody
parameter.
new Ajax.Updater('id_of_html_to_be_updated', 'url', {
parameters: {
id: 1,
name: "string"
}
});
// OR
new Ajax.Updater('id_of_html_to_be_updated', 'url', {
postBody: 'id=1&name=string'
});
In the first version prototype converts the parameters
option to a query string, while in the second example you explicitly state the query string in the postBody
parameter.
You supply them via the parameters
option:
new Ajax.Updater('targetId', '/your/url', {
parameters: {
foo: "fooValue",
bar: "barValue"
}
});
See the docs for details; docs for the various common Ajax options are here. The above updates the element with the ID "targetId" with the results of POST
ing the parameters foo
and bar
to /your/url
. Because I've supplied the parameters as an object, Prototype handles applying encodeURIComponent
to them for me.
精彩评论