javascript - how to call a service using ajax javascript?
问题描述
I'm learning programing, could you explain me how to call a service using ajax javascript?
Service information:
- Service type: REST
- Basic authentication
- Estructure: Application/JSON
- Url: https://osb.urosario.edu.co/uxxi-URO/WsFotografias/proxy/AdministradorFotografiasJsonPS/fotos/consultar
- User: Admi
- Password: admi
- Parameter JSON example: {"identificacion":["98122811999"]}
I've tested this service in postman
Service answer:
{
"respuesta": [
{
"estado": "Correcto.",
"identificacion": "98122811999",
"imagen": "return string Base 64 format"
}
]
}
解决方案
Using JQuery :
$.ajax({
type: 'POST',
url: 'https://osb.urosario.edu.co/uxxi-URO/WsFotografias/proxy/AdministradorFotografiasJsonPS/fotos/consultar',
dataType: 'json',
data:{"identificacion":["98122811999"]}
contentType: "application/json"
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', make_base_auth("admi", "admi"));
},
success: function (data,status) {
//do what you want with the data after success
//in this example the response will be promoted in the browser console
console.log(data);
});
});
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return 'Basic ' + hash;
}
推荐阅读
- kubernetes - 无法删除 Kubernetes 中的 apache solr operator CRD
- python - Scrapy 在更好的设备上的执行速度比在更差的设备上慢,我不明白为什么
- python - Xpath Python 从两个标题之间的表中提取数据
- python - 如何加入目录内的所有txt文件?(尊重所有行都在另一行之下)
- visual-studio-code - 如何使 cmd 成为打开 VS Code 时在 VSCode 中打开的默认终端。?
- vb.net - 使用 sendkey 命令 VBA 查看 Excel 公式
- node.js - 如何创建插件以在其他网站上销售我的产品?
- arrays - 为什么即使矩阵中存在元素,我也找不到输出元素?
- c - malloc_chunk 的布局到底是什么样的?
- javascript - 这个终端是什么意思?(使用 Next.js 下一个/路由器)