Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert jsonp to json [closed]

Here is my code

$.ajax({
    type: "GET",
    url: "http://example.com?keyword=r&callback=jsonp",
    success: function (data) {
        alert(data);
    },
    dataType: "jsonp",
    error: function (xhr, errorType, exception) {
        var errorMessage = exception || xhr.statusText;
        alert("Excep:: " + exception + "Status:: " + xhr.statusText);
    }
});

OK so the above code works fine and i'm getting a data as jsonp.Now i cant figure out how to convert jsonp to json.

like image 705
iJade Avatar asked Jan 10 '13 20:01

iJade


2 Answers

This article may give you some additional guidance: Basic example of using .ajax() with JSONP?

Can you provide us with an example of the data structure returned by the request?

In your particular circumstance, you could probably do something similar to the following. Let me know how this turns out:

// Create the function the JSON data will be passed to.
function myfunc(json) {
  alert(json);
}

$.ajax({
  type: "GET",
  url: "http://example.com?keyword=r&callback=jsonp",
  dataType: 'jsonp',
  jsonpCallback: 'myfunc', // the function to call
  jsonp: 'callback', // name of the var specifying the callback in the request
  error: function (xhr, errorType, exception) {
    var errorMessage = exception || xhr.statusText;
    alert("Excep:: " + exception + "Status:: " + xhr.statusText);
  }
});
like image 184
Joshua Burns Avatar answered Sep 27 '22 23:09

Joshua Burns


Now i cant figure out how to convert jsonp to json.

That's pointless. What you want is a plain javascript object to work with, and you already have that (data).


JSONP is a script file where a function is called with an object literal. The literal looks like JSON, and the function (whose name is dynamically generated) is the padding.

JSON is a file/string containing data in JavaScript Object Notation, a common serialisation format.

like image 41
Bergi Avatar answered Sep 27 '22 23:09

Bergi