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

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

问题描述

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

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 或#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 设置为新网址.

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.

使用 fetch() 接口的程序化 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 提交表单时,浏览器不会自动处理服务器响应.该响应返回到您的 Javascript,您的脚本决定如何处理它.如果您希望您的脚本遵循重定向,那么您必须检查响应,查看它是否为 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天全站免登陆