我必须调用一个REST URL,例如:https:// {baseurl} / employees / taxvalidation /。请求类型是JSON,但我总是得到错误警报。我无法弄清楚我的代码中有什么问题。我正在使用JQuery
支持的HTTP方法是:PUT(需要进行正确请求的HTTP PUT),还需要传递API密钥:XXXXX-XXXXX-XXXXX-XXXXX作为请求标头。
我在网页员工姓名和员工税中只有两个必填字段。我使用JQuery Ajax调用尝试了下面的内容。
申请正文样本:
"name": "Company XYZ", /* mandatory */
"TAX": "*********", /* mandatory */
"taxType": "U", /* Could be E, S, or U */
"address": "2501 Boilermaker road", /* optional */
"citystatezip":"Lapalma ca 76567", /* optional */
"country": "US", //optional
"checks" : "DT",`enter code here`
"details": "DT"`enter code here` //optional
$(function() {
$("#btnSubmit").click(function() {
alert("Hi JQuery");
var URL = "https://api.dev.amx-city.com/tesmdm/dev/tesmdm/empcatalog/partners/taxvalidation/";
$.ajax({
url: URL,
headers : {
'AMX-API-KEY': '487b9474-27a6-4d21-8eda-c3a2a14e4ebe'
},
type: 'POST',
data: {
name: 'Employeename',
tin: '79847324987',
tinType: 'U'
},
dataType: 'json',
success: function(result) {
alert(result);
},
error: function (restul) {
alert(result);
}
});
});
});
当我尝试按下按钮时,调试停止直到警报,之后我没有看到URL正在点击。如果我做错了,请告诉我?
我现在能够得到答复。以下是工作脚本
$.ajax({
url: URL,
type: "PUT",
headers: {
'AXT-API-KEY': '48776474-26a6-4d21-8eda-c3a2a14e4ebe'
},
data: JSON.stringify({"name": "SupplierName, LLC","tin": "522323454","tinType": "U","checks": "DT"
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr);
alert(thrownError);
}
});
当我在beforeSend函数中使用键时,它工作。但我不确定区别。
beforeSend:function(xhr){xhr.setRequestHeader(“AXT-API-KEY':'48776474-26a6-4d21-8eda-c3a2a14e4ebe”);