首页 > 解决方案 > 如何使用 JavaScript、json 或 ajax 调用 API

问题描述

搜索类型:方法: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?

标签: javascriptjsonajax

解决方案


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));

推荐阅读