I am trying to load the content (specific part) of an external webpage through an ajax request on my webpage. The curl URL for the request is as follows:
http: // useraname: password@X.X.X.X : PORT
So, I tried the following ajax call to get the web page
var username,password; $.ajax ({ type: "GET", url: "http://XXXX:PORT/", dataType: 'text/html', async: false, crossDomain: true, data: '{"username": "username", "password" : "secret"}', success: function (){ alert('Thanks for your comment!'); }, error: function (err){ alert(err); }, beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password)); } });
This gives me a CORS ( Cross-Origin Request Blocked: error. After changing dataType from text/html to jsonp . I received a response with the following error
[Exception ... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js :: .send :: line 5 "data: no]
The successful part of the ajax call fails anyway. Only this concerns part of the error. If I got the page, I can get the specific part in the following way.
var data = $.parseHTML(res); //<----try with $.parseHTML(). $(data).find('div.content').each(function(){ $('#here').append($(this).html());
How to get the desired result?
Following @GuRu's suggestion, I tried the following, but the success method is not called.
var username,password; $.ajax({ type: "GET", url: "http://XXXX:PORT/", data: '{"username": "user", "password" : "secret"}', async:true, dataType : 'jsonp', crossDomain:true, success: function (){ alert('Thanks for your comment!'); }, beforeSend: function( xhr ) { xhr.overrideMimeType( "text/plain; charset=x-user-defined" ); xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password)); } });