简体   繁体   English

在jquery中发送正常请求(不是ajax)

[英]Sending normal request in jquery ( not ajax)

Is there any method in jquery like $.post , $.ajax etc which works like a normal form submission. 在jquery中是否有任何方法,如$ .post,$ .ajax等,其工作方式与普通表单提交相同。 I know about .submit() but it requires a form element , can we send a normal request without a form element in jquery? 我知道.submit()但它需要一个表单元素,我们可以在jquery中发送没有表单元素的普通请求吗?

You can submit without a form using $.ajax() . 您可以使用$.ajax()在没有表单的情况下提交。 Further, to make it behave like a normal form would, set the async property to false . 此外,要使其行为与普通形式相同,请将async属性设置为false

$.ajax({
    url:   "/controller/action",
    data:  {'foo':'bar'},
    async: false
});

This will result in you being sent to: 这将导致您被发送到:

"/controller/action?foo=bar"
window.location.href = '/controller/action?foo=bar';

You are not clear on what you want to achieve. 你不清楚自己想要达到什么目标。 From what I understand, I'd suppose you want to send a GET or POST (form-submit-like) request to the server and make it seem like a real one. 根据我的理解,我想你想向服务器发送一个GET或POST(类似表单提交)请求,使它看起来像一个真实的请求。

In that case you would: 在这种情况下,你会:

  • Create a XMLHTTPRequest with appropriate parameters. 使用适当的参数创建XMLHTTPRequest。
  • Make it synchronous. 让它同步。
  • With the response, overwrite the DOM. 通过响应,覆盖DOM。

Not entirely clear what you are after, however you can use jQuery's serialize ( docs ) to pass a collection of values from input not in a form or any other items. 不完全清楚你所追求的是什么,但是你可以使用jQuery的序列化( docs )从输入中传递一组值而不是表单或任何其他项。 You would still need to post the data in some manner however. 但是,您仍然需要以某种方式发布数据。

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

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