jQuery post和html form post之间的区别 [英] Difference between jquery post and html form post

查看:213
本文介绍了jQuery post和html form post之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索一段时间,但我没有得到它:

im searching for a while but i dont get it:

Jquery $ .post和我用post提交普通的html表单有什么区别?

What is the different between a Jquery $.post and when i submit a normal html form with post?

我的jquery代码:

My jquery code:

$.post( "/test", query , function( data ) {
  console.log(data)  
});

我的html代码:

<form method="post" action="/test"   >
<input id="query" type="text"  name="q"  >
<input type="submit" value="Search">
</form>

我正在使用express,当我执行jquery时,下面的代码将无法工作.服务器只会将重定向发送为数据,而不会重定向页面.但是,当我使用表单时,它将重定向页面tp/newpage.为什么会这样呢?有没有一种方法可以使jquery像html表单一样工作?

Im using express and when i do the jquery post the code below wont work. The sever will just send the redirect as data and will not redirect the page. But when i use the form it will redirect the page tp /newpage. Why is that happening? Is there a way a that jquery will work the same way like the html form does?

router.post('/test', function(req, res) {
res.redirect('/newpage');
}

我看到可以在chrome中看到jquery使用类型"xhr",表单使用"document" ...我可以更改它吗?

I see can see in chrome that jquery use type "xhr" and the form use "document" ... can i change that?

推荐答案

Jquery $ .post和我用post提交普通的html表单有什么区别?

What is the different between a Jquery $.post and when i submit a normal html form with post?

在大多数情况下,什么都没有.两者都发出具有表单值的HTTP POST请求.一个只是在页面上下文中执行此操作,另一个只是在AJAX上下文中执行此操作,而无需刷新页面.

For the most part, nothing. Both issue an HTTP POST request with the values from the form. One just does so in the context of the page, the other does so in an AJAX context without refreshing the page.

服务器只会将重定向发送为数据,而不会重定向页面.

The sever will just send the redirect as data and will not redirect the page.

其中就有些误解.服务器从不重定向浏览器.服务器曾经做过的就是将标头发送回浏览器,提示浏览器执行重定向.

Therein lies a bit of a misunderstanding on your part. The server never redirects the browser. All the server ever does is send a header back to the browser suggesting that the browser perform a redirect.

当浏览器在页面加载请求的上下文中接收到该标头时,它将执行重定向.当JavaScript代码在AJAX请求的上下文中接收到该标头时,它将忽略它.根据设计,AJAX响应不会重新加载页面.

When the browser receives that header in the context of a page loading request, it performs the redirect. When the JavaScript code receives that header in the context of an AJAX request, it ignores it. AJAX responses, by design, don't reload the page.

如果您希望表单发布导致服务器启动的重定向,请使用常规表单POST.您仍然可以从JavaScript启动此表单POST,只是不使用$.post(),因为这是AJAX请求.相反,您可以执行$('form').submit()$('form input[type="submit"]').click()之类的操作.可能需要修补.

If you want the form post to result in a server-initiated redirect, use a normal form POST. You can still initiate this form POST from JavaScript, just not using $.post() since that's an AJAX request. Instead, you might do something like $('form').submit() or $('form input[type="submit"]').click(). It may take some tinkering.

另一方面,如果您要使用AJAX,则响应AJAX请求完成的任何条件重定向逻辑都必须位于客户端逻辑中.

If, on the other hand, you want to use AJAX then any conditional redirect logic in response to the completion of the AJAX request would need to be in the client-side logic.

这篇关于jQuery post和html form post之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆