I am getting json data as a response back from the server. I want to parse this JSON data and embed all the data into a pop up window.
var on_show_info = function() {
alert("aa");
request_meta_info = $.ajax({
url: data + info.id,
type: 'GET',
async: false,
开发者_StackOverflow dataType: "jsonp",
success: data,
error: error
});
};
var data = function(data, text_status, XMLHttpRequest) {
alert(data);
var html = '';
var info = {};
here I want that JSON parsing code as I am getting response back from the server in data field. Any help will be appreciated and I want to embed those parse data in to a popup window as shown below by this command `($('#popup-meta .popup-content').html(html);)`
});
$('#popup-meta').removeClass('waiting');
$('#popup-meta .popup-content').html(html);
};
This is the response I am getting back from the server as JSON data just for an example..
jsonp13082({"responseHeader":{"status":0,"Time":3,"params":{"json.wrf":"jsonp13082","wt":"json","q":"8377"}},"response":{"numFound":1,"start":0,"docs":[{"key":"83779616","number":"080","name":"Designated","name":"Non ","number":"27837","date":"2010-08-24T07:00:00Z","name":"Canada","name":"Application","title":"collision detection","date":"2008-03-03T08:00:00Z","id":"414","code":"CA","date":"2009-03-03T08:00:00Z","name":"Michael Henry","mgr_name":"William Henry","id":"79616","name":"oen","claims":"74","date":"2012-03-03T08:00:00Z","claims":"8","url":"","inventors":["D.","rshi","Pa"],"guid":["23","26","25"],"towners":["XYZ"],"inventors":["D","name2","name3"],"owners":["XYZ"]}]}})
var dataObj = jQuery.parseJSON( jsonStr );
See the link for more on jQuery (which u included) and converting jsons to data format.
http://api.jquery.com/jQuery.parseJSON/
How u use the data for the display is up to you and what data you are given.
You can use the jQuery native dialog box to display such a pop up
dialog in jQuery
In which you can do a for loop to convert each parameter into a display via HTML formatting (possibly using HTML tables).
function tablefy( object ) {
var retStr = "";
var typeSet = typeof(object);
if( typeSet == 'object' ) {
retStr += "<table>"
for( x in object ) {
retStr += "<tr>";
retStr += "<td>";
if( typeof( x ) == "string" ) {
retStr += x;
} else {
retStr += x.toString();
}
retStr += "</td><td>";
retStr += tablefy( object[x] );
retStr += "</td>"
retStr += "</tr>"
}
retStr += "</table>"
} else {
//just dumps it raw
if( typeSet == 'String' ) {
return object;
}
if( typeSet == 'number' ) {
return object.toString();
}
if( typeSet == 'boolean') {
if( object == true ) {
return 'true';
} else {
return 'false';
}
}
return 'null';
}
}
return retStr;
}
If you want to display it just plain text you'll have to convert it. try using http://code.google.com/p/jquery-json/
$.toJSON(data)
As such you can't display it using an alert box because as soon as it hits the client it automagically gets turned into a javascript object. Alert is fruitless.
Try instead
console.log (data)
and have a look at what is in your console window (ie - f12, chrome - ctrl shift j)
I think if you update your request's datatype from jsonp to json then the data object in the response handler should already be parsed, so
alert(data.name);
should work (if the returned object conforms to your example above)
EDIT: this answer based on the OP's indication that the response was json, but response appears to be jsonp
精彩评论