调用api时Ajax Post请求的形成

时间:2017-09-12 05:15:53

标签: javascript jquery ajax

我正在配置Ajax Post请求以命中api。

下面是curl命令(它的虚拟):

curl -X POST \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: token' \
--header 'timestamp: test' \
--header 'sourceSystemId: test' \
--header 'sourceSystemUserId: test' \
--header 'sourceServerId: test' \
--header 'trackingId: test' \
-d '{"accountid": "123456789","modeofBusiness": "Audio"}' \
'https://services-services.c1.com:443/api/customer/v1/services/querySummary'

需要改进这个以使用Ajax请求。这将是什么Ajax代码?如何在此传递--header-d标记?这个api以JSON格式返回响应。 下面是Ajax代码。

var accntno = document.getElementById('accntnoText').value;
var token = document.getElementById('tokenText').value;
//event.preventDefault();
var queryapi = "https://services-services.c1.com:443/api/customer/v1/services/querySummary";
$.ajax({
        url: queryapi,
        method: 'post',
        dataType: "json",
        data: { accountid: accntno, modeofBusiness: "Audio"},
        headers: { 'Authorization': token,'timestamp': 'test', 'sourceSystemId':'test','sourceSystemUserId': 'test', 'sourceServerId':'test','trackingId': 'test','Content-Type':'application/json'},
        success: function (data) {
                        alert(data);
}
});

1 个答案:

答案 0 :(得分:0)

首先:

  1. 你的curl有accountid,你的jQuery有accountNumber
  2. 您的http方法为post而非POST
  3. 您的jQuery标头缺失Accept并且在Content-Type
  4. 中有奇怪的空白
相关问题