7

I am trying to load the content(specific part) of external webpage through ajax request on my webpage. The curl url for the request is as follow

http://useraname:password@X.X.X.X:PORT

So, I tried the following ajax call to get the webpage

var username,password;
$.ajax
  ({
    type: "GET",
    url: "http://X.X.X.X: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 error (Cross-Origin Request Blocked:). After changing dataType from text/html to jsonp. The I received the 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 success part of the ajax call is not getting executed in either case. Only it goes to error part. If I received the page than I can fetch the specific part by the following method.

  var data = $.parseHTML(res);  //<----try with $.parseHTML().
  $(data).find('div.content').each(function(){
      $('#here').append($(this).html());

How to get the required result?

After suggestion of @GuRu, I tried the following, but still the success method is not getting called.

var username,password;
$.ajax({
  type: "GET",
  url: "http://X.X.X.X: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));
  }
  });
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Ajeet Khan
  • 8,582
  • 8
  • 42
  • 65

2 Answers2

2

For JSON text:

The MIME media type for JSON text is application/json. The default encoding is UTF-8. (Source: RFC 4627).

For JSONP with callback:

application/javascript

Here are some blog posts that were mentioned in the comments that are relevant.

Please check What is the correct JSON content type?

Community
  • 1
  • 1
Parth Trivedi
  • 3,802
  • 1
  • 20
  • 40
0

Try this, I thing that help you.

var username,password;
$.ajax({
  type: "GET",
  url: "http://X.X.X.X:PORT/",
  data: '{"username": "username", "password" : "secret"}',
  async:true, 
  dataType : 'jsonp', //you may use jsonp for cross origin request 
  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));
  }
});

This is work for me, try it using XMLHttpRequest. The XMLHttpRequest object is used to exchange data with a server.

var request = new XMLHttpRequest();
var params = "user=123&&password=456";
request.open('POST', 'https://www.example.com/controllers/Authentication.php', true);
request.onreadystatechange = function() {if (request.readyState==4) alert("It worked!");};
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");
request.send(params);
GuRu
  • 1,880
  • 3
  • 23
  • 32
  • This is causing error `Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource` – Ajeet Khan Dec 24 '15 at 17:54
  • please add this on ajax `async:true,` `dataType : 'jsonp', //you may use jsonp for cross origin request` `crossDomain:true,` – GuRu Dec 25 '15 at 08:10
  • I did that, the statusText is success but the `done` function is not getting called. Somewhere it is getting error. – Ajeet Khan Dec 25 '15 at 15:09
  • remove done function and put success function in Ajax – GuRu Dec 25 '15 at 16:03
  • I tried success function as well but still the `success` function is not getting called. see my last edit in the question what I tried. – Ajeet Khan Dec 25 '15 at 20:16
  • @Ajeet Khan please, see the link for the cross domain Ajax as like you http://icant.co.uk/articles/crossdomain-ajax-with-jquery/error-handling.html , I wish you get better solution – GuRu Dec 26 '15 at 09:07
  • i have tried almost everything, but still can't get the right things to work for me. BTW thankyou for your answers and help. :) – Ajeet Khan Dec 26 '15 at 09:24
  • @Ajeet Khan you can not getting success function because, I think your response is not formatted as per JSON. see the link http://stackoverflow.com/questions/2787180/jquery-ajax-success-callback-function-not-executed – GuRu Dec 28 '15 at 17:09
  • The response is going to be a html page – Ajeet Khan Dec 28 '15 at 17:21
  • I am try using XMLHttpRequest and its work, please see my answer – GuRu Dec 29 '15 at 12:38