简体   繁体   English

使用JS提交带有自定义HTTP标头的表单?

[英]Using JS to submit a form with Custom HTTP Headers?

I need to submit a form and include some custom http headers with the request (the API requires them). 我需要提交一个表单,并在请求中包含一些自定义的HTTP标头(API要求使用它们)。

Something like jQuery.ajax() works great ( https://api.jquery.com/jQuery.ajax/ ), I can use the beforeSend arg to compose some headers. 像jQuery.ajax()之类的东西很好用( https://api.jquery.com/jQuery.ajax/ ),我可以使用beforeSend arg来编写一些标头。

However I need this for a synchronous request, to direct the browser to the destination of the form. 但是我需要一个同步请求,以将浏览器定向到表单的目标。 So one of the following two would work, but I'm not sure if a solution exists. 因此,以下两种方法之一可以工作,但是我不确定是否存在解决方案。

  1. Have JS create and submit a form. 让JS创建并提交表单。 How do I include custom http headers in a form? 如何在表单中包含自定义http标头?

  2. Find an equivalent function to jQuery.ajax(), that will submit a synchronous request. 查找与jQuery.ajax()等效的函数,该函数将提交同步请求。

jQuery.ajax has a property async which can be set to true or false . jQuery.ajax具有async属性,可以将其设置为truefalse

async false would make it a synchronous request. async false将使其成为同步请求。

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

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