Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax & preemptive basic auth

I am trying to reach some RESTful services that are running under Apache preemtive basic authentication. I am using jquery Ajax, and sending the user and password with the 'Authentication' header. However, my request is throwing an empty error every time it runs.

Here is the full $.ajax call:

$.ajax({
      cache:false,
      url: urladdress,
      type:'GET',
      async:false,
      headers: { "cache-control": "no-cache" },
      dataType: "html", //or xml or json
      contentType: "html",
       beforeSend : function(req) 
       {
          req.setRequestHeader('Authorization', "Basic " +  base64string); //user:password);
       },
      success: function(data){
         successcallback(data);
      },
      error: function(xhRequest, ErrorText, thrownError){
           alert("ERROR: "+ JSON.stringify(xhRequest));
      },
      complete: function(result){
        ...
      }
});

What I am doing wrong? Is there something that I am ignoring here? Thanks.

like image 418
Gabriel Mendez Avatar asked Apr 23 '13 17:04

Gabriel Mendez


People also ask

What is jQuery AJAX?

AJAX = Asynchronous JavaScript and XML. In short; AJAX is about loading data in the background and display it on the webpage, without reloading the whole page. Examples of applications using AJAX: Gmail, Google Maps, Youtube, and Facebook tabs. You can learn more about AJAX in our AJAX tutorial.

Is jQuery and AJAX same?

AJAX is a web development technique for making asynchronous calls to the server. jQuery is a JavaScript library for designing and make some web development tasks easy. It makes it possible to run javascript outside of the browser. It works on the browser or outside the browser also.

Is AJAX better than JSON?

In advance, AJAX programming normally it returns the array of Java Object, which can be reused in JavaScript programming for designing interacting web pages. JSON is not using for only designing the web page. In fact, JSON sometimes not at all using for the web application.

Is AJAX built in jQuery?

Category: AjaxThe jQuery library has a full suite of Ajax capabilities. The functions and methods therein allow us to load data from the server without a browser page refresh.


1 Answers

The solution is on the server-side, you must include some custom headers in response to cross domain works. Experiment send the following headers in response:

Access-Control-Allow-Origin: yourApiDomain

Access-Control-Allow-Methods: *

  • In case of using custom headers:

    Access-Control-Expose-Headers: X-My-Custom-Header, X-Another-Custom-Header

    Access-Control-Allow-Headers: X-My-Custom-Header, X-Another-Custom-Header

  • In case of using HTTP Cookies and HTTP Authentication information:

    Access-Control-Allow-Credentials: true

For more informations read the documentation in MDN about CORS:

like image 177
rodmucha Avatar answered Oct 04 '22 05:10

rodmucha