首页 > 解决方案 > 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:

I've tested this service in postman enter image description here

Service answer:

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

标签: javascriptajaxrestweb-services

解决方案


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


推荐阅读