如何使用JavaScript,json或ajax调用API

时间:2019-02-04 10:07:07

标签: javascript json ajax

搜索类型: 方法:POST

URL:http:// // beta.etruckingsoft.com:8800/contact/searchContacts

身体:

{"Company Id" : 2, 
"SearchVal" : "b",
"Contact Type":"Broker",
"token" : "eyJhbGciOiJIUzI1NiJ9.MTkzMA.g132LGIzcwJaJRGa31q-k1hk6u79H0wIj1xjCJzLpZU"}

CONTACT-TYPE : application/json

如何使用JavaScript,ajax或json方法调用此API?

1 个答案:

答案 0 :(得分:0)

const xhr = new XMLHttpRequest(), url="http://beta.etruckingsoft.com:8800/contact/searchContacts", method="POST";
const body = {
    "Company Id" : 2,
    "SearchVal" : "b",
    "Contact Type":"Broker",
    "token" : "eyJhbGciOiJIUzI1NiJ9.MTkzMA.g132LGIzcwJaJRGa31q-k1hk6u79H0wIj1xjCJzLpZU"
};
xhr.open(method, url, true);
    xhr.onreadystatechange = () => {
        if(xhr.readyState === 4 && xhr.status === 200)
            console.log(xhr.responseText);
    };
xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(body));