Ajax request data as jsonp, but parse it as json - json

I have an issue trying to make a cross-domain ajax request for a json file. I can reach the file using jsonp, but on the client-side there is no jsonp response, and I have no control over changing that.
Is there a way to reach the file without cross-domain issues, through jsonp request, but then receive the response as json and parse it as json?

No, the response must be a function invocation. This is the basis of the JSONP convention. If you want access to some JSON data from your JSONP call, simply have the response invoke a function on your page, passing in the desired JSON data as a parameter.

Related

Sim808 + Arduino UNO: Get Json Object Response from Web Server

How to read JSON object response from the server?
These are the commands I use:
AT+SAPBR=3,1,"Contype","GPRS"
AT+SAPBR=3,1,"APN","internet"
AT+SAPBR=1,1
AT+SAPBR=2,1
AT+HTTPINIT
AT+HTTPSSL=1
AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","https://www.mywebsite.com/getTime?token=jsdaljdaskl"
AT+HTTPPARA="REDIR",1
AT+HTTPACTION=0
The returning request should be Json object, but the Sim808 read it as html response which gives error 606.
Need to define HTTP parameter content as JSON format using the following command
AT+HTTPPARA="CONTENT","application/json"
Found this document useful HTTP Client using SIM900.

Is it a good practice to send json data in http headers

I have a REST API Endpoint endpoint/action. The method is POST having a json data in the request body. I have two headers for this
Content-Type=application/json
Custom-Header={"object":{"id":"someId"}}
The object is deserialized during processing and the "someId" is used for other processes
Was wondering if it is a good practice to send json strings/data in the http header. Couldn't find anything in the Http Header documentations.
Option would be just sending in the "someId" and fetching the object using the id

Pass JSON object vs JSON string in HTTP POST

I'm building a REST API in JAVA and C# and I was wondering about the way I should pass data to those services.
What I'm familiar with as the right way is to send JSON object as the data in the POST body:
{name:'Dor'}
but I can also pass a string and parse the JSON in my service:
'{name:'Dor'}'
What is the preferable way from performance factor? or any other factors?
Basically, if you need to send the json data across via jquery, then we need to use stringify, else the data would be serialized into to key=value pair.
So, you cannot send the json object directly via jquery ajax method.
How it works behind the hood:
In $.ajax function, if we provide data as
data :{key1:"value1", key2:"value2"}
is serialized to key1=value1&key2=value2
if we provide data as
data :'{key1:"value1", key2:"value2"}' or JSON.stringify({key1:"value1", key2:"value2"})
is sent as {key1:"value1", key2:"value2"}
So, what we can conclude is that, we cannot pass json object directly via jquery, we can send only json string. Hope this clarifies everyone.

json returned from flickr api not getting parsed

I am trying to consume flickr api and requesting data in json format from a node.js server. I am providing nojsoncallback=1 so that returned json is not wrapped inside a jsonp callback. Here is the url http://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1.
Json returned is not properly formatted and JSON.parse is unable to parse it. If I don't pass nojsoncallback query param and eval the response which calls jsonFlickrFeed function, it gets evaled properly and I get the object. I also notice that the Content-type header in the response is application/x-javascript; charset=utf-8. I suspect that the special characters are double escaped in returned response.
I don't want to eval, what can I do to convert the response correctly into a string and then parse it?
By the way here is the gist with simplest use case. No modules, no middlewares.

How does server handle JSON in the body of a POST

I am using jQuery to post JSON data to a tomcat server and the server is handling the JSON array data perfectly! It is as though I passed key=value request parameters along with the URL.
So why am I posting this? I would like to know how the server treats JSON in the body of a request and how the data ends up being interpreted as request parameters. I have Googled my a** off and all I find is how the server sends JSON back to the client.
$.ajax() converts JSON data into key-value pairs (querystring style) by default. You need to set { processData : false } in the AJAX request to keep it as raw JSON.