javascript - 如何使用 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?
解决方案
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));
推荐阅读
- zig - 如何动态导入 zig 模块?
- apache-flink - 将 Hadoop 依赖项添加到独立的 Flink 集群
- javascript - 从文件路径 vue electron bulider 创建 blob
- python - TypeError:“模块”对象不可调用 - 导入错误?
- python - 如何通过python(Beautiful Soup)中的网络抓取来获取事件的日期?
- laravel - 在 Laravel 中不与 whereBetween 合作的地方
- javascript - Angular SSR,动态页面不会加载 - 状态 504
- ruby - Ruby:decode_www_form_component 因 %-encoding 无效而失败
- react-native - 地图功能内的输入文本不会改变本机反应
- python - 在python中计算字符串中的数字、字母和特殊字符