1

I opened another post last week because my elastic search was not returning accurate results see, ElasticSearch post

Basically what was happening was that when I am using jsonp, the request is not actually being sent as a GET request instead of a POST request. Below is the jsonp request. When I use json, it actually gets sent as a POST.

   amplify.request.define("searchPostRequest", "ajax", {
        url: "http://leServer:9200/people/person/_search",
        type: "POST",
        dataType: 'jsonp',
        contentType: 'application/json'
    });

Anyone know how I can force jsonp to be sent as a POST request?

Community
  • 1
  • 1
zmanc
  • 5,201
  • 12
  • 45
  • 90

1 Answers1

2

You can not make a JSONP call to a different domain or same domain with a POST since JSONP works by adding a script tag to the page. It is not making an XMLHttpRequest.

If you want to post data and it is the same domain, just make a normal POST ajax call to the server and return JSON.

If it is a cross domain call: If you control the other domain and you only care about modern day browsers you can use CORS, If not, you would have to use a proxy on your server to make the post request. Both of these solutions would also be making a JSON call, not JSONP.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • the odd thing is that both machines are in fact on the same domain, however IE does not send the request. Chrome has no issue though. – zmanc Jan 22 '13 at 14:22
  • @zmanc If that's the case, why resort to `jsonp` in the first place then? – Boaz Jan 22 '13 at 14:24
  • 3
    @zmanc note that a different port or protocol will count as a different domain. Ie `https://mysite.com:80` counts as a different domain to `http://mysite.com:81` – Rory McCrossan Jan 22 '13 at 14:25
  • JSONP still uses a GET request. It does not matter if it is on a different domain or same. – epascarello Jan 22 '13 at 14:25
  • @RoryMcCrossan, I think that might be the final piece to the puzzle. So if I drop the port requirement in the request it should be able to function as expected? – zmanc Jan 22 '13 at 14:27
  • You don't have to drop it (I assume you need it?), you need to ensure that both sites are on the same domain if possible. – Rory McCrossan Jan 22 '13 at 14:27
  • 1
    @zmanc If both domains conform to the same-origin-policy you can make a standard POST request from one to the other. See [this documentation](https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript) for clear and concise examples. – Boaz Jan 22 '13 at 14:30