javascript 在 jquery 中发送普通请求(不是 ajax)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10835715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:12:00  来源:igfitidea点击:

Sending normal request in jquery ( not ajax)

javascriptjqueryasp.net-mvc-3

提问by Gaurav

Is there any method in jquery like $.post , $.ajax etc which works like a normal form submission. I know about .submit()but it requires a form element , can we send a normal request withouta form element in jquery?

jquery 中是否有任何方法,如 $.post 、 $.ajax 等,它的工作方式类似于正常的表单提交。我知道.submit()但它需要一个表单元素,我们可以在 jquery 中发送一个没有表单元素的普通请求吗?

采纳答案by Darin Dimitrov

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

回答by Sampson

You can submit without a form using $.ajax(). Further, to make it behave like a normal form would, set the asyncproperty to false.

您可以在没有表单的情况下使用 提交$.ajax()。此外,要使其表现得像普通表单一样,请将async属性设置为false

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

This will result in you being sent to:

这将导致您被发送到:

"/controller/action?foo=bar"

回答by UltraInstinct

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.
  • Make it synchronous.
  • With the response, overwrite the DOM.
  • 使用适当的参数创建一个 XMLHTTPRequest。
  • 使其同步。
  • 使用响应覆盖 DOM。

回答by Matthew Riches

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. You would still need to post the data in some manner however.

不完全清楚您在追求什么,但是您可以使用 jQuery 的序列化 ( docs) 从输入中传递一组值,而不是在表单或任何其他项目中。但是,您仍然需要以某种方式发布数据。