0

I am trying to send AJAX requests to other servers using jQuery. I am operating locally. If I use HTML dataType I get the classical Origin http://127.0.0.1 is not allowed by Access-Control-Allow-Origin. So I started using JSONP requests :

$.ajax({
    url: 'SomeRemoteServer/SomeFile',
    dataType: 'jsonp',
    success: function(data) {
        // do stuff
    },
    error: function(d,msg) {
        alert(msg);
    }
});

The catched error is parsingerror and Chrome js debugger outputs Resource interpreted as Script but transferred with MIME type text/html. After carefully looking for this error on the web I found that the error came from the server and not my script (it should send back MIME type application/json or something close to this).

Well it seems however that the server is sending something... I would like to catch the response and self-process the parsing. Is this feasible ? If yes how ? I tried $.ajax() option converters but without success...

Charles
  • 50,943
  • 13
  • 104
  • 142
vanna
  • 1,552
  • 5
  • 20
  • 35
  • 2
    if the server is outputting text/html then are you sure the server intends you to send a jsonp request to it? – Ejaz May 01 '13 at 16:12
  • In my experience, "`Resource interpreted as Script but transferred with MIME type text/html`" is a *warning*, not an error. If your code isn't working, there must be something else wrong. (e.g., is the JSONP response from the server a valid script?) A JSONP response is a *script*; it is not JSON (though it may contain JSON in it somewhere) and should not use `application/json`. – apsillers May 01 '13 at 16:14
  • @apsillers : clearly it is not valid. I would like to insert my code before it even check its validity. I would just like to use some regex on the html source code of the url. – vanna May 01 '13 at 16:20
  • JSONP operates by injecting a new ` – apsillers May 01 '13 at 16:27
  • Thanks for the tips and links. Posted how I solved it. – vanna May 01 '13 at 16:43

1 Answers1

0

I solved my issue using dataFilter

$.ajax({
    url: 'SomeRemoteServer/SomeFile',
    dataType: 'jsonp',
    dataFilter: function(data) {
        // will run ok !
    }
    success: function(data) {
        // will fail
    },
    error: function(d,msg) {
        // will result in parsing error
    }
});
vanna
  • 1,552
  • 5
  • 20
  • 35