如何使用JavaScript调用API,JSON或AJAX [关闭]

问题描述 投票:-2回答:1

搜索类型:方法:POST

网址: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?

javascript json ajax
1个回答
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));
© www.soinside.com 2019 - 2024. All rights reserved.