post api调用和使用post方法提交表单之间有什么区别? [英] What is the difference between post api call and form submission with post method?

查看:445
本文介绍了post api调用和使用post方法提交表单之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用支付网关,因为该支付网关使用表单提交方法调用,方法为post,我可以使用来自节点js HTTP模块的post API调用来调用同一网关,我很困惑,我无法使用post API的调用网关导致它不会重定向到新页面,并且表单具有methodaction可以通过post call重定向到新页面?

I want to call payment gateway, for that payment gateway is called using form submission with the method as post, Can I call the same gateway using post API call from node js HTTP module, I am confused, that I cannot call gateway using post API cause it won't redirect to new page, and form have method and action which can redirect to new page with post call?

推荐答案

有多种方法可以从浏览器提交表单:

There are multiple ways to submit a form from the browser:

  1. HTML表单,提交按钮,用户按下提交按钮,不涉及Javascript.
  2. 页面中的HTML表单,Javascript获取表单的DOM元素并在表单对象上调用.submit()方法.
  3. 使用XMLHttpRequest接口和POST方法进行Ajax调用,并手动发送适当的表单数据.
  4. 使用POST方法进行Ajax Fetch调用并手动发送适当的表单数据.
  1. HTML form, submit button, user presses submit button, no Javascript involved.
  2. HTML form in the page, Javascript gets DOM element for the form and calls .submit() method on the form object.
  3. Ajax call using the XMLHttpRequest interface with the POST method and manually sending appropriate form data.
  4. Ajax Fetch call with the POST method and manually sending appropriate form data.

使用#1或#2,浏览器发送表单,浏览器将注意重定向并在浏览器中显示表单响应(无论是否重定向).

With #1 or #2, the browser sends the form and the browser will pay attention to redirects and will display the form response (whether redirected or not) in the browser.

对于#3和#4,该表单通过Javascript发送,并且响应返回到您的Javascript. #3不处理重定向. #4具有处理重定向的选项.以下是上述每个选项的更多信息. #3和#4完全不会影响浏览器的显示,除非您自己编写Javascript处理请求并影响浏览器的显示(通过插入内容或将window.location设置为新的URL.

With #3 and #4, the form is sent via Javascript and the response comes back to your Javascript. #3 does not process redirects. #4 has an option to process redirects. Here's more info on each of the above options. #3 and #4 do not affect the browser display is not affected at all unless you program your own Javascript to process the request and affect the browser display (either by inserting content or setting window.location to a new URL.

以下是有关上述方案的更多信息:

Here's some more info on the above schemes:

使用XMLHttpRequest进行程序化Ajax调用不会以任何方式处理重定向或Ajax调用的响应.他们只是将该响应返回给您的Javascript.请记住,重定向只是您可以从Ajax调用中获得的响应的一种特定类型.这与浏览器提交的POST表单不同.

Programmatic Ajax calls with XMLHttpRequest do not process redirects or the response from the Ajax call in any way. They just return that response to YOUR Javascript. Keep in mind that a redirect is just one specific type of response you can get back from an Ajax call. This is different than a browser submitted form POST.

编程Ajax调用提供了自动跟随重定向的选项.请参见redirect选项此处.但是,即使在这种情况下,所有fetch()接口所做的都是获取重定向URL的内容.它不会导致浏览器页面更改.为此,您必须编写自己的Javascript代码以查看3xx重定向响应,然后将window.location设置为新的重定向URL.或者,您必须让界面自动遵循重定向,然后对新的重定向内容进行一些处理,使其返回到Javascript.

Programmatic Ajax calls with the fetch() interface offer an option to follow redirects automatically. See the redirect option here. But, even in this case, all the fetch() interface does is get the contents of the redirected URL. It does not cause the browser page to change. To so that, you would have to write your own Javascript code to either see the 3xx redirect response and then set window.location to the new redirect URL. Or, you would have to let the interface follow the redirect automatically and then do something with the new redirected content that it will return to your Javascript.

这些程序化请求不同于让浏览器为您提交表单.在浏览器提交的情况下(不使用Javascript提交表单),浏览器将根据表单响应返回的内容遵循重定向并更新浏览器中的显示.

These programmatic requests different than letting the browser submit a form for you. In the browser submitted case (without using Javascript to submit the form), the browser follows redirects and updates the display in the browser based on whatever content is returned from the form response.

当您通过Ajax提交表单时,浏览器不会对服务器响应自动执行任何操作.该响应返回到您的Java脚本,然后您的脚本决定如何处理它.如果希望脚本遵循重定向,则必须检查响应,查看其是否为3xx状态,从相应的标头中获取新URL并将window.location设置为该新URL.然后,这将导致浏览器显示重定向页面.但是,您必须自己编写程序,或者找到一个提供执行此功能的Ajax库.一个标准的Ajax调用只是将POST响应形式返回给您的Javascript-就这样.您的脚本必须处理该响应并决定下一步要做什么.

When you submit a form via Ajax, the browser does nothing automatically with the server response. That response goes back to your Javascript and your script decides what to do with it. If you want your script to follow redirects, then you have to examine the response, see if it's a 3xx status, get the new URL from the appropriate header and set window.location to that new URL. That will then cause the browser to display the redirect page. But, you have to either program that yourself or find an Ajax library that offers a feature to do it form. A standard Ajax call just returns the form POST response back to your Javascript - that's all. Your script has to process that response and decide what to do next.

我很困惑,我无法使用post API调用网关,因为它不会重定向到新页面

I am confused, that I cannot call gateway using post API cause it won't redirect to new page

可以.您只需要编写自己的Javascript来处理来自编程API调用的响应,如果重定向为3xx,则将window.location设置为新URL,以指示浏览器加载新的重定向页面.

You can. You just need to write your own Javascript to process the response from the programmatic API call and, if its a 3xx redirect, then set window.location to the new URL to instruct the browser to load the new redirected page.

这篇关于post api调用和使用post方法提交表单之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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