繁体   English   中英

使用 asp.net/JQuery Ajax 调用 REST API url

[英]Call the REST API url using asp.net / JQuery Ajax

我必须调用 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");

暂无
暂无

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

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