有没有办法提交“假”?使用jQuery形成? [英] Is there a way to submit a "fake" form using jQuery?
问题描述
我想做的是使用任意参数发出POST请求,就像提交表单一样。那不仅仅是做XHR 。我理想的是模拟提交(如提交表单,而不是POSTiing然后重定向)。我想避免$ .post的延迟+ window.top.location = blah
最直接的方法是使用'假'形式并插入一堆元素,然后序列化并使用jQuery.submit(),类似于有没有办法使用jQuery提交一个没有精心设计的字段的表单?
<我希望有一种更优雅的方式。
我的用例是我通过外部API调用(来自Facebook和其他服务)收集大量随机字段,并希望一次性向用户提交所有这些信息创建,好像用户填写了所有这些信息并按下提交
有什么建议吗?谢谢!
是
你可以使用$ .ajax( 示例)
或$ .post和$ .get ...示例:
$ .post(
url_to_page,
/ *像这样的字符串:text = me%20man& mail=peace@man.com或对象:* /
{text:me man,mail:peace@man.com}
)
/ *您可以检查请求是否已成功提交* /
.success(函数(数据){
alert(成功!);
alert(数据) ); //页面上的数据
})
/ *你可以检查请求是否失败* /
.error(function(){
alert(Error!)) ;
});
What I want to do is make a POST request with arbitrary params, much like submitting a form. That is not merely doing the XHR. What I ideally want is to emulate a submit (like submiting a form, not POSTiing and then redirecting). I want to avoid the latency of $.post + window.top.location = blah
The most direct way would be to have a 'fake' form and insert a bunch of elements, then serialize and use jQuery.submit(), something like Is there a way using jQuery to submit a form without the elaborate field by field breakdown?
I wish there were a more elegant way.
My use case is that I am gathering a bunch of random fields through external API calls (from Facebook and other services) and want to submit ALL that info at once to user creation, as if the user filled out all this information and pressed 'submit'
Any suggestions? Thanks!
yes
you can use $.ajax (example)
or $.post and $.get ... example:
$.post(
"url_to_page",
/* string like this: "text=me%20man&mail=peace@man.com or an object": */
{text: "me man", mail: "peace@man.com"}
)
/* you can check if the request was submitted successfully */
.success(function(data) {
alert("Success!");
alert(data); //data on page
})
/* you can check if the request failed */
.error(function() {
alert("Error!");
});
这篇关于有没有办法提交“假”?使用jQuery形成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!