簡體   English   中英

如何使用 ajax javascript 調用服務?

[英]how to call a service using ajax javascript?

我正在學習編程,你能解釋一下如何使用 ajax javascript 調用服務嗎?

服務信息:

我已經在 postman 中測試了這項服務在此處輸入圖像描述

服務解答:

{
    "respuesta": [
        {
            "estado": "Correcto.",
            "identificacion": "98122811999",
            "imagen": "return string Base 64 format"
        }
    ]
}

使用 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; }

您可以使用以下方法調用上面的 RestEndpoint:

xmlhttp.open("POST", "/EndpointURI", true);
 
xmlhttp.onreadystatechange = function() 
{
    if (this.readyState == 4 && this.status == 200) 
    {
        //Use parse() method to convert JSON string to JSON object
        var responseJsonObj = JSON.parse(this.responseText);
 
        //use response
    }
};
 
var jsonData = {"name" : "yourData"};
xmlhttp.send( JSON.stringify( jsonData ) ); 

對於身份驗證,請使用:

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://EndPointURI", true);
xhr.withCredentials = true;
xhr.setRequestHeader("Authorization", 'Basic ' + btoa('userName:password'));
xhr.onload = function () {
    console.log(xhr.responseText);
};
xhr.send();

對於身份驗證部分,使用 JQuery 這樣將易於實現和理解。 現在沒有人使用基本的 xmlhttp 在 javascript 中調用 api,我上次使用的是 2003 開發的應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM