Unable to parse JSON when making JSONP call - json

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\">"}

Related

Node.js express res.json behavior

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.

API endpoint returns text before and after the JSON content, causing parsing error

I'm using postman to test calling a rest service endpoint.
I'm trying to parse the JSON return content but it throws an error because the response body has more than just JSON.
This is how I parse it in my postman test script:
var jsonData = JSON.parse(responseBody);
Here is the response body:
--13398550-b6ea-4731-a8ee-4b2ad24c3cfe
Content-Type: application/json; charset=utf-8
//this is the actual content I want to parse --->
{"id":"123456","value":"the_value"}
--13398550-b6ea-4731-a8ee-4b2ad24c3cfe--
When I try to parse it, I get the following error (in postman)
There was an error in evaluating the test script: SyntaxError:
Unexpected number in JSON at position 3
Obviously because the content being parsed is not just JSON
Is this something special that the api is doing? Or am I just parsing it incorrectly?
NOTE: I'm not including details of the rest service function. If the cause of this issue is something that is being done by the service itself, then that is enough of an answer for me to perhaps ask another question or do some further investigation. The purpose of this question is to ask whether this is something special being done in HTTP, or if it's the service.
Edit:
I managed to see the server side code and it is indeed manually building the response with boundaries identified by a GUID. I'll have to manually parse the response
The server is not emitting straight up application/json, it's packed in a multipart mime envelope.
Whether or not it's doing that correctly depends on the response headers. If you didn't expect a multipart response, but a simple JSON response, then I'd say yes: it's something you need to fix server-side.

$.getJSON ParseError

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.

Json RPC error 32600

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.

json-rpc malformed request with

I'm building an application that communicates with a django backend using json-rpc. So far all has been working well. However I've found an anomaly in sending " ". As far as I know the request works fine, however django interprets the response badly. I've reproduced a simplified request and response below:
Request:
{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello there"}}
Django receives:
<QueryDict:u'{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello ': [u''], u'nbsp': [u''], u'there"}}': [u'']}>
Expected response:
<QueryDict: {u'{"jsonrpc":"2.0","id":"1","method":"test","params":
{"id":"80","name":"tests","introduction":"hello there"}}': [u'']}>
It seems like django interprets the & and the ; as special characters and so creates an unexpected dictionary in its request.POST variable.
What do I need to do to make sure that the json string doesn't get malformed? I have tried encoding it using the php htmlspecialchars() method, but since that doesn't remove the '&' the problem persists.
Any help will be much appreciated.
Django is handling the (POST?) request by decoding the body (your json string) as if it were a query string, and not a json.
Within a query string, & and ; denote the end of a key:value pair. Splitting up your request body on those two characters yields the key:value pairs you see in the Django QueryDict.
You need to get hold of the POST request body and explicitly decode it to a dict yourself using either the standard lib json, or simplejson module.
I have little experience with Django specifically, but I imagine that somewhere in your view handler you would do something akin to:
try:
data = json.loads(requesst.raw_post_data)
## work with the data...
except ValueError:
## do something...
No doubt Django provides a way to move this json handling out of your views, and to somewhere more suitable.