json parser error while making ajax request? - java

Folks i am getting "parsererror" exception while making ajax request to server.On my dev box
its working fine but at pruction getting this issue for some users(not reproducible at local box)
I did google and found one of the probable cause can be data format mismatch . For example server is
sending jsonp format and client expecting json format or vice versa.But this is not the case with me
becoz its json at both cient and server side.
From serverside struts 2 converts java object(which contains list of map ) in to json string internally.so
I see no scope of error here.
So i suspect there is something in data .for example :=data like scott" but i coud not reproduce issue
even with this kind of data .Can you guys help me to identify what kind of data inside
json can cause this issue/ or it can be a different issue altogether?
$.ajax({
"url": myURL,
"success": function (json) {
},
"dataType": "json",
"cache": false,
"error": function (xhr, error, thrown) {
if (error == "parsererror") {
alert("Getting Error");
}
}
});

Related

Not able to post data to server and not geting any respons from server

I am not able to post data to server. On clicking button i submit form and i am getting data into api service class but it's not sending to server which place somewhere else. I am going to give all detail here under:I have other apis method in service class they are working well , but when i want to post data, i am not able to post data and neither getting any response from server while checking database no entry there:
service.ts : orgId is string which is pass in url as foreign key to add adresspostmodel , and data is json object. I am using data:any(reason the actual table has more field which auto generated). when i use postman the api and all working well with same url and json object as i giving hereunder:
service.ts :
public postFormData(orgId: string, data: any): Observable<AdressModel> {
return this.http.post<any>(`${this.ApiUrl}/${orgId}/addresses`, data).pipe(
tap(response => console.log(response)), catchError(this.handleError));
}
onSubmit(){
this.data =
{
"city": "test",
"email": "test#gemiil.com",
"name": "test2",
"recipient": "test",
"street": "test",
"zipCode": "12345"
}
this.ApiService.postFormData(this.organizationId, this.data);
}
this.data actually i am getting data from form, but here form example i am giving you json mock object, which is also correct, here is my json object and whole api in console both api , och json object in console is looking correct for me :
api data in console
{
"city":"test",
"email":"test#gemiil.com",
"name":"test2",
"recipient":"test",
"street":"test",
"zipCode":"12345"
}
Can anyone help to find what's problem is there with logic?
Observable are lazy in nature, so you must subscribe to make it to send request to server.
this.ApiService.postFormData(this.organizationId, this.data).subscribe((resp) => console.log(resp));

jax-rs consuming org.json.JSONObject respsonds with Exception

I am trying to build a rest api that should consume json/x-application data. Now I have looked into two libraries javax.json-api and org.json for handling the data.
Example JSON:
{
"error": "false",
"error_msg": "",
"version": "1.13.10",
"result": {
"malware": {
"finding1": {
"file": "/path/to/filep",
"malware": "{HEX}r2h.malware.blue.44"
}
}
},
"newest_version": "1.13.10"
}
If I now consume this with javax JsonObject, it will work and I can go on with my code. BUT, if i instead post this data and I use org.json.JSONObject I will receive response at the client:
Unrecognized field "error" (class org.json.JSONObject), not marked as ignorable
Tried to find responses on the web, but I didnt step over anything that explains this?
Regards and Thanks
Well,
I do not really know if there is a solution for this. Eventually the REST architectural style does not support JSONObject (org.json.JSONObject). However, the workaround is pretty easy, just consume the json as a String (still you can declare HTTP Request to enforce the type application/json).
So this could look like the following:
#Path("/myendpoint")
#POST
#Consumes(MediaType.APPLICATION_JSON)
public String receiveRequest(String json) {
JSONObject jo = new JSONObject(json);
...
}
First off all your json is valid json. Second thing you are getting a error in response, it means error is not in your code. And third thing the error is "Unrecognized field" it means the POJO class inside the client code does not contain field "error".

JSONRPC format different between Jsonrpc4j and go's rpc/jsonrpc

I was meet a problem when I tried used net/jsonrpc package to build a server and a Java client with jsonrpc4j
The problem is jsonrpc4j is when error happen, golang`s method will return error and encoding to json.
I got this json object in client
{"id": -6028374044949000, "result": null, "error": "some error return message"}
This object cast failed in java's json4j.
http://www.jsonrpc.org/specification#error_object
After I checked the jsonrpc page, it is said the error field MUST a json object with fields [code, message, date], the golang jsonrpc package not meet the require.
So I`m confused how to solve this.
Change the jsonrpc lib,
Just replace the rpc way to thrift/gRpc,
Avoid to return error but send error in reply and let Java check the response,
Or just edited the json4j or golang's source code ( I'm very horrible about this option)
Thanks for watch.
If you need JSON-RPC 2.0 support for Go you can try https://github.com/powerman/rpc-codec

JQuery was not Called

I am getting errors on callBacks. I have tried following code in jsfiddle.com . You can also try. Data from servelet is not returning. It's returning same error again and again. Check jquery library when you try in jsfiddle
$.ajax({
url : 'http://192.168.16.111:8081/MiddleWareUsman/androidServlet',
type : "post",
dataType: "jsonp",
data : {
"fname": "chaaaaapiio",
"lname": "gya"
},
success : function(data) {
alert("hello"+data);
},
error : function(xhr, ajaxOptions, thrownError) {
alert(thrownError);
}
});
My server side:
String a=request.getParameter("fname");
String b=request.getParameter("lname");
response.getWriter().write(a+" "+ b);
It appears you have a couple problems.
JSONP requests can't be sent via POST. They are actually sent as <script> tag requests anyway which are GET requests.
Your server isn't doing JSONP. For the server to do JSONP, it must wrap the requested data in a call to a javascript function who's name was passed as an argument to the request and then the actual data is passed as an argument to that function. JSONP is a big hack, but it works by requesting a javascript and that's what the server must return.
you simply can't send a POST request using JSONP
check this link out to see how JSONP works..

Rest API method is returning value but still receive 500 error

I'm working on a Java Jersey REST API and a website using this API.
This is my server side method
#GET
#Produces(MediaType.APPLICATION_JSON)
public List<TreeViewModel> getTreeList() {
User user = User.getByCredentials("team", "team");
List<TreeViewModel> list = user.getTreeViewModels();
return list;
}
And this is my javascript request
function requestTrees() {
$.ajax({
type: "GET",
url: window.api + "tree",
dataType: 'json',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", make_base_auth('team', 'team'));
},
success: function(data) {
console.log('suc');
},
complete: function(jqxhr, txt_status) {
console.log('com');
},
failed: function(data) {
console.log('fai');
}
});
}
If I debug my server the method gets called and returns the list. But at client side I receive a 500 error which indicates something is wrong at server side?
Any suggestions?
Thanks in advance.
500 means internal server error, and should usually be accompanied by log messages or even in some cases an exception embedded in the message body sent to the client.
What has most likely happened is that your method has processed and returned the List<TreeViewModel> but that could not then be serialized for sending.
For example some serializers do not like working with raw collections. You need to have your own object and then put the list within that. The only way to find the actual cause of the problem though is to find that exception and see what it is telling you.

Categories