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?
Well generally JSONP is implemented by adding a
<script>
tag to the calling document, such that the URL of the JSONP service is the "src". The browser fetches script source with an HTTP GET transaction.Now, if your JSONP service is in the same domain as your calling page, then you could probably cobble something together with a simple
$.ajax()
call. If it's not in the same domain, then I'm not sure how it'd be possible.You could use a CORS Proxy using this project. It would direct all traffic to an endpoint on your domain and relay that information to an external domain. Since the browser is registering all requests to be on the same domain we are able to post JSON. NOTE: This also works with SSL certificates held on the server.
There's a (hack) solution I've did it many times, you'll be able to Post with JsonP. (You'll be able to Post Form, bigger than 2000 char than you can use by GET)
Client application Javascript
JAVA:
PHP:
Doing like this, you are opening your server to any post request, you should re-secure this by providing ident or something else.
With this method, you could also change the request type from jsonp to json, both work, just set the right response content type
jsonp
json
Please not that you're server will no more respect the SOP (same origin policy), but who cares ?
If you need to send a lot of data cross-domain. I usually create a service that you can call in two steps:
First the client do a FORM submit (post allowed cross domain). The service stores the input in the session on the server (using the GUID as key). (the client creates a GUID and send's it as a part of the input)
Then the client do a normal script-inject (JSONP) as a parameter you use the same GUID as you used in the FORM post. The service processes the input from the session and returns the data in the normal JSONP-fashion. After this the session is destroyed.
This of course relies on that you write the server-backend.
I know this is serious necromancy, but I thought I'd post my implementation of JSONP POST using jQuery, which I'm successfully using for my JS widget (this is used for customer registration and login):
Basically, I'm using an IFrame approach, as suggested in the accepted answer. What I'm doing differently is after sending the request, I'm watchin, if the form can be reached in the iframe, using a timer. When the form cannot be reached, it means the request has returned. Then, I'm using a normal JSONP request to query for the status of the operation.
I hope that someone finds it useful. Tested in >=IE8, Chrome, FireFox and Safari.
It is possible, here is my solution:
In your javascript:
In your url.php: