简体   繁体   English

带有JavaScript原型的Ajax请求

[英]ajax request with javascript prototype

如何通过原型库中定义的ajax.updater通过post方法直接附加参数?

The implicit method used by prototype is POST and you got two possibilities to submit post data. 原型使用的隐式方法是POST ,您有两种提交提交数据的可能性。 Either via the parameters option, or via the postBody parameter. 通过parameters选项,或通过postBody参数。

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. 在第一个版本中,原型将parameters选项转换为查询字符串,而在第二个示例中,您在postBody参数中明确声明查询字符串。

You supply them via the parameters option: 您可以通过parameters选项提供它们:

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 . 各种常见Ajax选项的文档在这里 The above updates the element with the ID "targetId" with the results of POST ing the parameters foo and bar to /your/url . 上面的代码通过POST将参数foobar的结果更新为ID为“ targetId”的元素到/your/url Because I've supplied the parameters as an object, Prototype handles applying encodeURIComponent to them for me. 因为我已将参数作为对象提供,所以Prototype会为我处理将encodeURIComponent应用于它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM