I have a JQ/JS function that creates an AJAX request, and returns a JSON result.
function ajaxRequest(url, callback){
$.ajax({
url: "Data/"+url,
type : 'GET',
contentType: "application/json",
success: function( resp ) {
callback(resp );
}
});
}
Currently this passes the whole response to my callback function.
How can I parse this for just the JSON object.
Each JSON object is different, they are a single level object, but each with different parameter names.
I was wondering if there is a generic way to pass just the json object.
Typically, I would use:
resp.ObjectName
but I want to make this generic, for instances where I do not have ObjectName.
Please try the dataType
option instead of contentType
:
function ajaxRequest(url, callback){
$.ajax({
url: "Data/"+url,
type : 'GET',
dataType: "json",
success: function( resp ) {
callback(resp );
}
});
}
You need to use $.parseJSON
for parsing json result:
function ajaxRequest(url, callback){
$.ajax({
url: "Data/"+url,
type : 'GET',
dataType: "json",
success: function( resp ) {
var response = $.parseJSON(resp)
callback(response);
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.