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

时间:2019-03-26 21:18:54

标签: c# rest api

我必须调用一个REST网址,例如:https:// {baseurl} / employees / taxvalidation /。 Request类型是JSON,但我总是收到错误Alert。我无法弄清楚代码中的错误。我正在使用JQuery

支持的HTTP方法是:PUT(需要进行带有正确请求的HTTP PUT),而且我还需要传递API密钥:XXXXX-XXXXX-XXXXX-XXXXX作为请求标头。

我在网页Employee name和Employee Tax中只有两个必填字段。 我已经尝试过使用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被点击。让我知道我做错了什么吗?

2 个答案:

答案 0 :(得分:0)

我现在可以得到答复。下面是工作脚本

$.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);
    }
});

答案 1 :(得分:0)

当我使用beforeSend函数中的键时,它起作用了。但是我不确定有什么区别。

beforeSend:函数(xhr){                 xhr.setRequestHeader(“ AXT-API-KEY':'48776474-26a6-4d21-8eda-c3a2a14e4ebe”);