I'm using $.getJSON and I get this error "Invalid character".
My server side returns a good looking json, and in fiddler I think it also looks fine.
What am I doing wrong?
Thanks.
You have a mix of XML and JSON in your response. This obviously does not work. It needs to be valid JSON only.
Related
My website is using ajax calls to add products to cart. Each time a customer presses "Add to Cart" button, there's an ajax request called. The Json data response is sometimes not valid or not formed correctly.
Using firefox developer tools, here's the response data in both ways:
Normal json response:
Not valid json data response:
1) What kind of issue is this?
2) Why is this happening in some cases and not other cases? Could it be the data itself causing this?
3) Possible solutions to this?
In general there are two possible cases where browser can't parse JSON data:
Wrong Content-Type header
Malformed JSON string
In your case, as it sometimes works and sometimes doesn't it's probably the second one. There must be some characters in your response that are escaped in your server side code which are not valid in browser. All server side language have options when converting objects to JSON strings. you can check the invalid response in a JSON linter like https://jsonlint.com/ to see which part causes the problem then search for options to disable this behaviour in your server side code.
1) What kind of issue is this?
Server side issue.
2) Why is this happening in some cases and not other cases?
Bad logic in the server side backend code.
Could it be the data itself causing this?
No
3) Possible solutions to this?
Fix the server side code's logic.
You should check if the datatype of your Ajax function is JSON and you should check your server side code, perhaps the response is not well formatted.
the problem in you post parameters sometimes sending value or sometimes not check the code of javascript and server code as well for validation.
When tried parsing the below data in client using JSONP (regular ajax call is looking good.)getting error because it is having a html content.
My server codes supports JSONP calls.
EG : JSON.parse('{"key":"<input type="text">"}')
Error : SyntaxError: Unexpected token t
Can some one help to over come this.
It looks like your server doesn't escape strings when bake JSON. It should looks like:
{"key":"<input type=\"text\">"}
I'm using SJCL, and it works fine with small ASCII strings.
But when I try to decode this piece of JSON (the result of the encryption of an HTML page) I get a "this is not JSON!" error.
The JSON has been produced by SJCL, and while I did encode it and decode it using LZW and base64 I don't get this error for small strings with the same workflow.
I tracked the error message origin to the decode function. I assume the regexes are failing but I don't understand why as this seems to be a perfectly formed JSON string to me.
However, I can be wrong as if I do a JavaScript eval on it it fails on a syntax error. But if I dump it in a file Python parse it fine.
The json that is at your this piece of json link starts and ends with a double-quote character. Is that actually part of the contents of the json? If it is, I believe that is your problem. Otherwise, it looks like valid json to me.
Ok I made a double passed base64 encoding. One before encryption, and one after. It seems that removing the first pass make it work.
I am using jquery ajax to pass json rpc request to remote server. Here is my json string:
{"jsonrpc":"2.0","method":"merchant_check","params":{"hostID":150999,"orderID":107,"amount":"7777","currency":"051","mid":15001038,"tid":15531038,"mtpass":"12345","trxnDetails":""},"id":107}
I am getting this error:
{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid JSON-RPC 2.0 request error (-32600)"}}
What am I doing wrong? Thanks for help.
It may be the JSON liberary the service is using. Try the following:
Put spaces between the end of a key string+colon amd tje value, "key": "value" vs "key":"value"
Try putting the request id as a string, "id": "1" vs "id": 1
I don't know how well ist was coded, the service, but if you have malformed parameters, it may give you the-32600 error, instead of -32602. So what kind of currency are you using that requires no decimal and gets sent as string? What is a "mid", an integer?
Your request object looks good, it think it's what the service is expecting and calling good or bad request object.
I had the same problem using json-rpc net. The problem was caused by the content-type header. By default it was being set to application/x-www-form-urlencoded by my browser and it generated that error. Setting it to application/json fixed my issue.
I have implemented webservice and call this webservice using JSON.
But i got error like "JSON.parse: bad control character in string literal"
Please give me a solution.
Thanks in advance.
Can you provide an example of what your webservice is returning when you call it?
It sounds as though you've got an invalid character in the JSON string, which is preventing it from being parsed, but without more detail I can't really provide you a better answer.