I am using JSON to send data from my server to my javascript/html5 client. I use org.codehaus.jettison.json.JSONObject to encode objects and strings on the server side and jquery/ajax to unencode. This has worked fine until today, when I tried to send an inequality symbol ("less than or equal to") from server to client. On the client side, I got this error:
message:
"Unexpected end of input."
When I remove the inequality and re-try, the string goes through fine. I suppose I could create my own special encoding for the 'or equal to' inequality symbols, but that seems to go against the happy grain of leaving encoding up to the parser.
Interesting to note that I do not have the reverse problem: When I 'stringify' from the js side and send a string with an 'or equal' inequality via ajax/json to the server, there is no problem with unencoding on the server side.
Thanks for any help you can offer.
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.
I have been working on one of my projects using node and ng4
when I came across using res.json to send an array back to Angular 4 front end, like res.json(arrayResult). Surprisingly, I cannot use JSON.parse(arrayResult) after Angular receives it because it throws an error saying unexpected end of input. I however can access all the data through result[i], just like any normal array.
I don't quite understand why res.json() does not send my array as a string to the front end. Is there any internal conversion involved? Why I could access the content through index without even parsing it or doing any conversion with it?
The server indeed send your json data as a string. Additionally to the string, the server passes Content-Type header to your client which tells your client what kind of data you received.
So if the Content-Type was text/html your client would think that he received an HTML file.
In your case, res.json using Content-Type: application/json which tells the client that the string that he got is actually a json object, so no need for you to use JSON.parse.
You can see the Content-Type under the response headers property.
I have a server running that runs on TCP/IP. It reads strings and responds with strings. I just wondered if I can just connect via Flash to my server and get some answers from it. My second idea was:
var socket: Socket = new Socket("192.168.0.100", 4847);
socket.writeObject("hello");
var answer: String = socket.readObject();
trace(answer);
Connection is established successfully. But I'm not sure how I send and receive strings now.
Update:
socket.writeUTFBytes("hello\r\n"); seems to work for sending
how to read ? socket.readUTF() ?
I don't know how long the answer might be, it can be short or very long
how about end of line ? It is important for my server since that's how messages are separated. Do I have to send eol via "\r\n" ?
Update 2: This seems to work well
It depends how your server handles the requests and repsonses. For sending and receiving strings use the readUTFBytes and writeUTFBytes.
If you want to use the functions writeObject and readObject your server must know how to handle the AMF serialization. You can find libraries for different languages on this wikipedia page http://en.wikipedia.org/wiki/Action_Message_Format and implement AMF on the server . If you are working on a larger project I would personally recommend that.
I am getting JSON response from some web server, say the server returns:
"kən.grætju'leiʃən"
I use AFNetworking and JSONKit, but what I've received is:
"æm'biʃən"
Not sure if it's AFNetworking's problem or JSONKit's problem, but any way, how to I parse and convert the string so it looks the same as from server?
Thanks
The server may be returning characters encoded in a way that violates the official JSON spec. If those characters are encoded as escaped unicode IDs (like \U1234) then JSONKit and NSJSONSerialization should both handle them fine.
If you can't change the server, you can work around the issue by URL-decoding the string - see https://stackoverflow.com/a/10691541/1445366 for some code to handle it. But if your server isn't following the correct specs, you're likely to run into other issues.
I have an app that I'm working on converting from CF8 to CF10 and some of my remote CFCs where the data coming back should be JSON are now failing because there seems to be a "//" pre-pended to the returned data. For example here's an output of a returned structure:
//{"SUCCESS":true,"ERRORS":[],"DATA":{"COLUMNS":["AUTHRESULT","SPID","EMAIL","RID"],"DATA":[[true,361541,"user#domain.com",""]]}}
The same function run through the same CFC on the CF8 server gives:
{"ERRORS":[],"SUCCESS":true,"DATA":{"COLUMNS":["AUTHRESULT","SPID","EMAIL","RID"],"DATA":[[true,361541,"user#domain.com",""]]}}
The CFC that proxies all requests does have returnFormat="JSON" - but there is no SerializeJSON() being called in either the proxyCFC or the CFC that is extended from proxyCFC.
I'm not sure what's the best way to handle this. Trimming off the '//' in the response would be possible but it doesn't seem "right". I need to address it on the CF10 end of things because these functions are in use not only in our app, but some remote apps as well (and some are through http:// posts and some are through jQuery Ajax calls).
That is a server side setting in the ColdFusion admin, under settings. Prefix serialized JSON with. It is enabled by default for security. Protects web services, which return JSON data from cross-site scripting attacks by prefixing serialized JSON strings with a custom prefix.. Perhaps you had turned this off on your ColdFusion 8 server. I do not recommend turning it off though.
See this post from Raymond Camden - Handling JSON with prefixes in jQuery and jQueryUI
NOTE: this setting can also be set per-application by setting secureJSON and secureJSONPrefix in your Application.cfc file. See the documentation about that here - Application variables.
secureJSON - A Boolean value that specifies whether to add a security prefix in front of the value that a ColdFusion function returns in JSON-format in response to a remote call.
The default value is the value of the Prefix serialized JSON setting in the Administrator Server Settings > Settings page (which defaults to false). You can override this value in the cffunction tag.
secureJSONPrefix - The security prefix to put in front of the value that a ColdFusion function returns in JSON-format in response to a remote call if the secureJSON setting is true.
The default value is the value of the Prefix serialized JSON setting in the Administrator Server Settings > Settings page (which defaults to //, the JavaScript comment character).