Asked  6 Months ago    Answers:  5   Viewed   29 times

Is it possible to post data to JsonP? Or does all data have to be passed in the querystring as a GET request?

I have alot of data that I need to send to the service, cross domain, and it is too large to send via the querystring

What are the options for getting around this?

 Answers

39

It is not possible to do an asynchronous POST to a service on another domain, due to the (quite sensible) limitation of the same origin policy. JSON-P only works because you're allowed to insert <script> tags into the DOM, and they can point anywhere.

You can, of course, make a page on another domain the action of a regular form POST.

Edit: There are some interesting hacks out there if you're willing to go to a lot of effort inserting hidden <iframe>s and mucking about with their properties.

Tuesday, June 1, 2021
 
Karsten
answered 6 Months ago
93

JSONP is not JSON. A JSONP response would consist of a JavaScript script containing only a function call (to a pre-defined function) with one argument (which is a JavaScript object literal conforming to JSON syntax).

The response you are getting is JSON, not JSONP so your efforts to handle it as JSONP fail.

Change dataType: 'jsonp' to dataType: 'json' (or remove the line entirely, the server issues the correct content-type so you don't need to override it).

Since your script is running on a different origin to the JSON then you will also need to take steps (most, but not all, of which require that you control the host serving the JSON) to work around the same origin policy.

Wednesday, June 9, 2021
 
MKM
answered 6 Months ago
MKM
65

you can use getJSON for example

$.getJSON('ajax/test.json', function(data) {
  $('.result').html('<p>' + data.foo + '</p>'
    + '<p>' + data.baz[1] + '</p>');
});

check complete getJSON documentation http://api.jquery.com/jQuery.getJSON/

EDIT

I was wrong... using Jquery.ajax will cause cross-browser issue but not with Jquery.getJSON

http://docs.jquery.com/Release:jQuery_1.2/Ajax#Cross-Domain_getJSON_.28using_JSONP.29

Here is an example of cross-domain get JSON

EDIT

Firefox has a problem with HTTPS, as i know it will be fixed if you send your request like this

$.getJSON('ajax/test.json',{}, function(data) {
  $('.result').html('<p>' + data.foo + '</p>'
    + '<p>' + data.baz[1] + '</p>');
});

Source: AJAX https POST requests using jquery fail in Firefox

Hope this helps

Saturday, October 9, 2021
 
a_horse_with_no_name
answered 2 Months ago
58

This is a little trickier than normal since it's a Django website, and we need to deal with Django's Cross-site Request Forgery protection by generating a CSRF token.

Here's how to do it with httr, using the example file provided here:

library(httr)
csrf <- GET(url='http://ionspectra.org/aristo/batchmode/')$cookies$csrftoken
res <- POST(url='http://ionspectra.org/aristo/batchreport/', 
            body=list(batchfile=upload_file('example.txt'),
                      format='tsv',
                      csrfmiddlewaretoken=csrf))
out <- read.delim(file=textConnection(content(res)), 
                  stringsAsFactors=FALSE)

The GET call generates the CSRF token, which is needed for the subsequent POST call.

Thursday, October 28, 2021
 
Linkgoron
answered 1 Month ago
41

I found the problem.

My post code uses $.ajax(...), as shown in initial message.

But jquery's .ajax post data as application/x-www-form-urlencoded. This worked in previous versions of WebAPI, but in latest update it seems it doesn't accept application/x-www-form-urlencoded by default.

I changed content type to application/json, and I had to convert data to json, and this solved the problem.

So, instead of

j$.ajax({
    type: "POST",
    url: uri,
    data: dataObj,
    success: function(...)

I had to change to

j$.ajax({
    type: "POST",
    url: uri,
    data: JSON.stringify(dataObj),
    contentType: "application/json; charset=utf-8",
    success: function(...)
Tuesday, November 2, 2021
 
Anton Barinov
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share