I'm running this getJSON async call in jQuery to get the data back into and then out of my Java code. For whatever reason I continually get the error:
"Ajax Error: Error invoking generateSearchQuery([object Object])"
I've debugged both the jQuery and the Java code. The Java code receives the JSON data, processes it, and writes it back without a problem. The jQuery code kicks the error when it gets into the internal getJson method and tries if(json.successful)
.
The response is: {"error":{"message":"","title":"Error Generating Search Query"},"data":[Ljava.lang.String;@1e40c9f,"successful":"successful"}
I've been working on this async call for a few days now, but still can't get it working. Any ideas?
jQuery function:
generate : function(){
getJson(
Search.guid,
"generateSearchQuery",
{
"data": [
"val0",
"val1",
"val2",
"val3",
"val4"
]
}, function(json) {
Search.data = $.parseJSON(json.data);
$.each(Search.data, function(k, v){
$('#searchQuery').append(v + "\n");
});
});
},
Java function:
@AsyncService(name = "generateSearchQuery", permission = "")
public void generateSearchQuery(HttpServletRequest req, HttpServletResponse res) {
res.setHeader("Cache-Control", "no-cache");
JSONResult result = new JSONResult("Error Generating Search Query");
String[] arr = req.getParameterValues("data[]");
for(int i = 0; i< arr.length; i++){
arr[i] = "success"+i+"!";
}
result.put("data", arr);
try {
res.getWriter().print(result);
} catch (IOException e) {
// whatever
}
}
}
getJson:
function getJson(guid, service, parms, callback) {
beginWork();
$
.ajax( {
url : document.URL,
type : 'GET',
data : parms,
cache : 'false',
dataType : 'json',
headers : {
'framework-guid' : guid,
'async-service' : service
},
success : function(json) {
endWork();
if (json.successful) {
callback(json);
} else {
error(json.error && json.error.title ? json.error.title
: 'No Title Provided', json.error
&& json.error.message ? json.error.message
: 'No message provided for service ' + service);
}
},
error : function(jqXHR, textStatus, errorThrown) {
endWork();
error('Ajax Error', 'Error invoking ' + service + '('
+ parms + ')');
}
});
}
It seems that the content of the response from server wasn't json format. Because you specify the type of response of $.ajax as json, so you need to convert object result to json. You can use google gson http://code.google.com/p/google-gson/ , and do something like the followings.
Gson gson = new Gson();
res.getWriter().print(gson.toJson(result));
And in getJson, you should make the following changes.
//before
Search.data = $.parseJSON(json.data);
//after
Search.data = json.data;
The response doesn't seam to be valid!
I checked it, you have an invalid label there Ljava.lang.String;@1e40c9f , the error i get when parsing your code is:
SyntaxError: invalid label
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.