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.
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.
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With