4

I'm trying to make a JSONP request to allow cross-domain. The situation is that my server DOES NOT handle JSONP request, and it understands this request as JSON and responds back with a JSON object: {result: 1}

Here is my ajax request:

jQuery.ajax({
    type : 'POST',
    url : "https://example.com/addUser",
    data : {
        firstName : 'firstName',
        lastName : 'lastName',
        email : 'email@yopmail.com'
    },
    crossDomain : true,
    jsonpCallback: "jsonpCallback",
    success : function(data) {
        alert(data);
    },
    complete : function(jqXHR, textStatus) {
                //can do something here
    },
    error : function (xhr, textStatus, errorThrown) {
        if(errorThrown=="jsonpCallback was not called") {
                console.log('caught it!');
        }
            },
    dataType : 'jsonp'
});

Readings from the window console:

Resource interpreted as Script but transferred with MIME type text/json: "https://example.com/addUser…Name=firstName&lastName=lastName&email=email%40yopmail.com&_=1359646137496".

Uncaught SyntaxError: Unexpected token : 

caught it!

As expected, it throws parseerror exception and I try to handle it. But my question is that since the browser is actually getting the response {result:1}, can there be a way I could parse it?

Pulkit Mittal
  • 5,916
  • 5
  • 21
  • 28
  • Did u try to change data like: `var jsonBuilder = JSON.stringify(data : { firstName : 'firstName', lastName : 'lastName', email : 'email@yopmail.com' });` ? – Maxim Shoustin Jan 31 '13 at 15:39
  • 3
    If the server does not support JSONP, you can not make a JSONP request to it. End of story. The only way would be to proxy the request through a script on your server. – gen_Eric Jan 31 '13 at 15:39
  • What happens if you try to `console.log` the `jqXHR.responseText` in the `complete` method or the `xhr.responseText` in the `error` method? – Ian Jan 31 '13 at 15:41
  • Note: When using JSONP, the `jsonpCallback` parameter is the name of the function that the JSON will be wrapped in. This function needs to exist. It's suggested to leave that parameter off and let jQuery do it for you. If you want to change what GET parameter is added to the JSONP request, use the `jsonp` parameter. – gen_Eric Jan 31 '13 at 15:43
  • @MaximShoustin: The data is being sent fine, I have problems in parsing the response back. – Pulkit Mittal Jan 31 '13 at 15:43
  • @RocketHazmat: I guess that is what I understand as well. I am looking for some out-of-the-box solution here, because it is very difficult for me to make changes on the server end. I seriously believe there has to be a way to parse the data, because I AM getting exactly what is required. – Pulkit Mittal Jan 31 '13 at 15:44
  • @Ian: It says `undefined`. – Pulkit Mittal Jan 31 '13 at 15:46
  • 5
    There is no way. JSONP is simply including a JavaScript file from a different URL using a dynamically created `script` element, e.g. ``. If `some/script` does not return valid JavaScript you are out of luck. JSON is not valid JavaScript. JSONP has to be supported by the server. Maybe helpful: http://stackoverflow.com/q/2067472/218196 and http://stackoverflow.com/q/3076414/218196. – Felix Kling Jan 31 '13 at 15:46
  • @PulkitMittal: JSONP is *very* different from JSON. A JSONP "request" isn't actually an AJAX request. JSONP actually adds a ` – gen_Eric Jan 31 '13 at 15:46
  • 2
    @PulkitMittal: You have 2 options. 1) Edit the server to output a JSONP response (wrap the JSON in a function call, and use the `text/javascript` content type), or 2) make a JSON (AJAX) request to the same sever that this page is on, and use a proxy script to get the JSON from the other server. – gen_Eric Jan 31 '13 at 15:54
  • Thank you friends. I might have to avoid making the JSONP request altogether then, because I am unable to change server's request handler. – Pulkit Mittal Jan 31 '13 at 15:58
  • If the server does not handle jsonP then you can't make a jsonp call - doesn't matter whether it is cross-domain or not, the server still has to support jsonp.You could dynamicallyt determine if it is another domain in javascript and call either json or jsonp, but for the cross-domain version to work the server it is calling must support jsonp. In other words, if the server does not support jsonp then there is no point in using it to try and get around domain restrictions – Adam Feb 14 '13 at 06:12

1 Answers1

-1

use datatype:'json'

$.ajax({
 url: getinstruments,
  dataType: 'json',
  success: function(data) {
  alert (data.firstName);
}
});
Nawaz
  • 303
  • 1
  • 4
  • 15