如何使用带有附加标头的 JQUEry 调用 Rest API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25085012/
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-08-26 11:02:49  来源:igfitidea点击:

How to call Rest API with JQUery with additional Headers

jqueryrestheader

提问by Mayur

I want to give a call to citrix Rest API for webinar registration.

我想调用 citrix Rest API 进行网络研讨会注册。

Here is the URL for API:

这是 API 的 URL:

https://api.citrixonline.com/G2W/rest/organizers/{organizerKey}/webinars/{webinarKey}/registrants

The request should be POST and it should have additional headers

请求应该是 POST 并且它应该有额外的标头

"Accept", "application/json"
"Content-type", "application/json"
"Authorization", "OAuth oauth_token=ACCESSTOKEN"

I tried to make a AJAX call to this API but i am getting a Network 403 Error

我试图对此 API 进行 AJAX 调用,但出现网络 403 错误

My code looks like this:

我的代码如下所示:

$.ajax({
  url : url,
    dataType : "jsonp",
    headers: { "Content-Type":"application/json","Accept": "application/json","Authorization": "OAuth oauth_token=ACCESSTOKEN" },
    type : 'POST',
    contentType: "application/json",

    data : {"firstName" : "FirstName", "lastName" : "lastNAme",
      "email" : "[email protected]"},

    success : function (data) {
      console.log(data);
    },
    error : function (data, errorThrown) {
      alert(3);
    }
});

Please help!!

请帮忙!!

回答by Ivan

According to the jQuery ajax() documentation,

根据jQuery ajax() 文档

headers (default: {})

Type: PlainObject

An object of additional header key/value pairs to send along with requests using the XMLHttpRequest transport. The header X-Requested-With: XMLHttpRequest is always added, but its default XMLHttpRequest value can be changed here. Values in the headers setting can also be overwritten from within the beforeSend function. (version added: 1.5)

标头(默认:{})

类型:普通对象

要与使用 XMLHttpRequest 传输的请求一起发送的附加标头键/值对的对象。标头 X-Requested-With: XMLHttpRequest 总是被添加,但它的默认 XMLHttpRequest 值可以在这里更改。标题设置中的值也可以从 beforeSend 函数中覆盖。(版本添加:1.5)

You can set headers for your requests using the following according to Prestaul:

您可以根据Prestaul使用以下内容为您的请求设置标头:

// Request with custom header
$.ajax({
    url: 'foo/bar',
    headers: { 'x-my-custom-header': 'some value' }
});

Make sure that you actually provided the correct ACCESSTOKEN.

确保您确实提供了正确的 ACCESSTOKEN。

回答by Uresh Kuruhuri

Did you try using setRequestHeaderthe as a part the ajax call?

您是否尝试setRequestHeader将 用作 ajax 调用的一部分?

xhr.setRequestHeader

example below:

下面的例子:

$.ajax({
  url : url,
    dataType : "jsonp",
    headers: { "Content-Type":"application/json","Accept": "application/json","Authorization": "OAuth oauth_token=ACCESSTOKEN" },
    type : 'POST',
    contentType: "application/json",
    data : {"firstName" : "FirstName", "lastName" : "lastNAme", "email" : "[email protected]"},
    beforeSend : function( xhr ) {
        xhr.setRequestHeader( "Authorization", "BEARER " + access_token );
    },
    success : function (data) {
      console.log(data);
    },
    error : function (data, errorThrown) {
      alert(3);
    }
});

I hope it would help.

我希望它会有所帮助。

回答by Manmohan Soni

You can try this.

你可以试试这个。

$.ajax({
  url : url,
    dataType : "jsonp",
    headers: { "Content-Type":"application/json","Accept": "application/json","Authorization": "OAuth oauth_token=ACCESSTOKEN" },
    type : 'POST',
    contentType: "application/json",
    data : {"firstName" : "FirstName", "lastName" : "lastNAme", "email" :         "[email protected]"},

beforeSend : function setHeader(xhr){
                            xhr.setRequestHeader('X-VN-ad', "sdd");
                            xhr.setRequestHeader('X-VN-APs', "mei");
                        }