简体   繁体   English

使用Jquery AJAX的POST标头和正文

[英]POST headers and body using Jquery AJAX

I have to send a POST to a broker API including headers and a body: 我必须将POST发送到包含标头和正文的代理API:

Action  POST https://demo-api.ig.com/gateway/deal/session

Header  
Content-Type: application/json; charset=UTF-8
Accept: application/json; charset=UTF-8
X-IG-API-KEY: 5FA056D2706634F2B7C6FC66FE17517B

Body    
{ 
"identifier": "A12345", 
"password": "112233" 
} 

The API is here: https://labs.ig.com/rest-trading-api-guide 该API位于此处: https//labs.ig.com/rest-trading-api-guide

The problem is that every example on the internet sends the data as "data" like this: 问题是互联网上的每个示例都将数据作为“数据”发送,如下所示:

jQuery.ajax(

{

url : 'https://demo-api.ig.com/gateway/deal/session',

type: 'POST',

dataType : "json",

data: {identifier: "A12345", password: "112233"}

});

I don't understand this, where is the header and the body? 我不明白,标题和正文在哪里? All examples look basically like this, I can't make heads or tail out of this. 所有示例基本上都像这样,我无法对此一目了然。

The data is for the post parameters in your case, headers have their own object. 数据用于您所用的post参数,标题具有其自己的对象。

jQuery.ajax(

{

url : 'https://demo-api.ig.com/gateway/deal/session',

type: 'POST',

dataType : "json",

headers: {"X-IG-API-KEY":"5FA056D2706634F2B7C6FC66FE17517B"},

data: {identifier: "A12345", password: "112233"}

});

Using beforeSend call back function and set headers. 使用beforeSend回调函数并设置标题。 try below code - 尝试下面的代码-

jQuery.ajax({
  url : 'https://demo-api.ig.com/gateway/deal/session',
  type: 'POST',
  dataType : "json",
  data: {identifier: "A12345", password: "112233"}
  beforeSend: function(xhr){xhr.setRequestHeader('X-Header', 'header-value');},    
});

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

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