21

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?

Danny Lin
  • 180
  • 8
Sriks
  • 1,669
  • 6
  • 26
  • 40

3 Answers3

45

If you are doing cross domain request:

$.ajax({
    url: "yoururl",
    type: "GET",
    dataType: 'json',
    xhrFields: {
         withCredentials: true
    }
});
mwcz
  • 8,949
  • 10
  • 42
  • 63
jiantongc
  • 941
  • 1
  • 11
  • 14
  • 1
    Note that some embedded browsers (like the web-view in Filemaker) that suppress the user/pass prompt, so this will not work for everyone. – Gram Jun 21 '16 at 18:12
14

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);
     },
 });
Mortalus
  • 10,574
  • 11
  • 67
  • 117
  • 1
    Note: the above won't work for digest authentication. See http://stackoverflow.com/questions/5288150/is-digest-authentication-possible-with-jquery#5288679 – mpr Jun 01 '16 at 18:08
0

My 2 cents on this.

I wasted a ridiculous amount of time on this issue, but I solved it by changing my request's method from a GET to a POST.

The strange thing is that both method types (GET/POST) work when called via Postman but it only managed to hit my Web API endpoint when I changed it to a POST method.

Note that in my case, I'm only passing the grant_type as I'm using Windows Authentication so I didn't to pass a username or password when posting.

Thierry
  • 6,142
  • 13
  • 66
  • 117