Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass credentials for a webservice using jquery ajax call?

I am using the jquery ajax call like this:

$.ajax({
         url: WEBSERVICE_URL,
            type: "GET",
            dataType: "application/json; charset=utf-8",                   
            username: "admin",  // Most SAP web services require credentials
            password: "admin",
            processData: false,
            contentType: "application/json",
            success: function() {
               alert("success");
            },
            error: function() {
                   alert("ERROR");
                },
    });

still the call is not going to web service. Everytime I am getting ERROR alert. Can some body help me on this please?

like image 320
Sriks Avatar asked Jan 29 '13 09:01

Sriks


People also ask

Does AJAX work with https?

You cannot make an AJAX request to an https page if you are currently in http because of the Same Origin Policy. The host, port and scheme (protocol) must be the same in order for the AJAX request to work.

Do AJAX calls pass cookies?

AJAX calls only send Cookies if the url you're calling is on the same domain as your calling script. This may be a Cross Domain Problem.


2 Answers

If you are doing cross domain request:

$.ajax({
    url: "yoururl",
    type: "GET",
    dataType: 'json',
    xhrFields: {
         withCredentials: true
    }
});
like image 144
jiantongc Avatar answered Sep 18 '22 12:09

jiantongc


Try using post for the method type, most webservices are secured and require transimssion using post and not Get

plus to help you debug the error and a response text to your error.

 $.ajax({
     url: WEBSERVICE_URL,
     type: "POST", //This is what you should chage
     dataType: "application/json; charset=utf-8",
     username: "admin", // Most SAP web services require credentials
     password: "admin",
     processData: false,
     contentType: "application/json",
     success: function () {
         alert("success");
     },
     error: function (xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response
         alert(xhr.status);
         alert(xhr.responseText);
     },
 });
like image 26
Mortalus Avatar answered Sep 17 '22 12:09

Mortalus