Search code examples
javascriptangularjshttp-postjsonp

Send data using POST to JSONP request


Summary :

Want to send data to another domain using POST method to JSONP.

Code :

$http.jsonp($scope.baseApiUrl+'QueryBuilder/getData?callback=JSON_CALLBACK')
.success(function(data, status, headers, config) {
    $scope.success = true;
    console.log(data);
}).error(function (data, status, headers, config) {
    $scope.error = true;
});

Its working fine. But I have a lot of data that I need to send to the service, cross domain, and it is too large to send via the querystring. So I want to send it as JSON.

data: {
    "dbName":"test",
    "tables":["city","persons"],
    "filters":["city_id='Hyd'"]
}

Findings :

How to make a jsonp POST request that specifies contentType with jQuery?
Unable to post data using JSONP on Cross Domain
How to send data using JSONP from localhost to domain.com
Using PUT/POST/DELETE with JSONP and jQuery

Every post shows that it is not possible.so is there any alternate way to do this ?

Question :

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

Any immediate help will be highly appreciable. Thanks.


Solution

  • Is it possible to post data to JSONP?

    No, it isn't.

    JSONP works by adding a <script> element that gets data from its src (the data is embedded in a JavaScript program). You can't make a POST request to load a JavaScript program.

    Or does all data have to be passed in the querystring as a GET request?

    Yes


    JSONP is a dirty hack to get around the Same Origin Policy.

    CORS was introduced as standard way to get around the Same Origin Policy that is much, much more flexible. You can make POST requests with XMLHttpRequest if authorised by CORS. Use CORS instead.