0

I am doing a cross domain get using jquery ajax but i get success as status in the error function. I am expecting data to be returned from the get service is :

document.write("<div class=\"display_archive\"><div class=\"campaign\">07\/18\/2013 - <a href=\"http:\/\/us6.campaign-archive2.com\/?u=c7b9b2f23eb4f63fgfgf5ac4f&id=99e1f5f249\" title=\" Newsletter #3\" target=\"_blank\">Newsletter #3<\/a><\/div>");

but what i get is in error function

Object { readyState=4, status=200, statusText="success"}

My code

getResource : function ( containerSelector,serviceURL ) {
    $.ajax ({
        type : "GET",

        dataType: 'jsonp',

        jsonpCallback: 'jsonCallback',

        url : serviceURL,

        headers : {
            "Accept" : "application/json",
            "Content-Type" : "application/json"
        },

        success : function successData(resourceObj) {
            console.log("success ");
            console.log(resourceObj);
        },

        error : function errorData(resourceObj) {
            console.log("error ");
            console.log(resourceObj);
        },

    })
},

Am i missing something here ? help please

inputError
  • 600
  • 3
  • 12
  • 26
  • http://stackoverflow.com/questions/18956854/ajax-call-to-a-webservice-aways-fails/18958068#18958068 – dileep kommireddy Sep 25 '13 at 14:29
  • Yes, you're missing the error parameter (it's the 3rd parameter passed to the error callback.) – Kevin B Sep 25 '13 at 14:29
  • using json padding as data-type, AJAX **error:** function will not be called. – ram Sep 25 '13 at 14:29
  • @ram but... it is being called. In some circumstances it does still get called. – Kevin B Sep 25 '13 at 14:30
  • @KevinB the resource object gets me the whole object from the server do i still need to pass extra parameters ? however the issue is why does it not returns me the data ? – inputError Sep 25 '13 at 14:33
  • @KevinB.. Also note the data i am expecting is not json but it is text/html – inputError Sep 25 '13 at 14:33
  • 1
    @inputError Yes, the third parameter will give you jQuery's error message. It will tell you what (if anything) jQuery thinks the problem is. If you're expecting text/html, jsonp is not the correct dataType, therefore jQuery will send you to the error callback with the third parameter set to "parseerror" because it couldn't parse the response as json. – Kevin B Sep 25 '13 at 14:35
  • @KevinB i see. If datatype is not jsonp then how would i be able to make a cross domain call? – inputError Sep 25 '13 at 14:38
  • you can't. simple as that. You either must use JSONP, implement CORS, use iFrame postmessages, or use a server-side proxy. The only method out of those methods that can be done without changing the service-server is the server-side proxy. – Kevin B Sep 25 '13 at 14:40
  • @KevinB I cant control what the server sends so there is no way out? – inputError Sep 25 '13 at 14:41
  • 1
    Yes, a server-side proxy is your only way out if you have no control over the remote server. Make a service on your server that requests data from the remote server, and have your javascript request from the service on your server. `javascript -> your server -> remote server -> your server -> javascript` – Kevin B Sep 25 '13 at 14:41
  • @KevinB . Thanks Kevin will try to implement that – inputError Sep 25 '13 at 14:43
  • @KevinB ah you beat me to it - server side proxy is the way to go, JSONP is a nasty hack at best and using a server-side proxy instantly cuts out all cross domain BS plus gives a chance to transform the data in cases when it's not even the correct format. – Stephen Byrne Sep 25 '13 at 14:44

2 Answers2

1

Try adding:

crossDomain: true,

I can't see any error here other than that.

Gidil
  • 4,137
  • 2
  • 34
  • 50
bios
  • 243
  • 1
  • 4
  • 17
  • that parameter is very rarely ever needed. If this indeed is a cross-domain request, that parameter is already set to true. – Kevin B Sep 25 '13 at 14:37
1

Use JSONP for Cross-domain requests

because Cross-domain requests are not directly allowed. However, there is a commonly-used technique called JSONP that will allow you to avoid this restriction through the use of script tags. Basically, you create a callback function with a known name:

function getData(data) {       
}

And then your server wraps JSON data in a function call, like this:

getData({"the": "data"});

And you "call" the cross-domain server by adding a script tag to your page. jQuery elegantly wraps all of this up in its ajax function.

Another technique that I've had to use at times is cross-document communication through iframes. You can have one window talk to another, even cross-domain, in a restricted manner through postMessage. Note that only recent browsers have this functionality, so that option is not viable in all cases without resorting to hackery.

Refrence 1

Refrence 2

jQuery AJAX cross domain

Community
  • 1
  • 1
Ishan Jain
  • 8,063
  • 9
  • 48
  • 75