1

I have a small function with jQuery Core 2.1.4:

function ajax(url, method, data, success) {
  $.ajax({
    contentType: "application/json",
    data: JSON.stringify(data),
    dataType: "json",
    method: method,
    processData: false,
    url: url
  });
}

And then this code:

var myPage = "http://localhost/blankResponse";
var myFunc = function(data) {
  alert(data);
};

ajax(myPage, "POST", {}, myFunc);   // Request to "testpage" with body
ajax(myPage, "PUT", {}, myFunc);    // Request to "testpage" with body
ajax(myPage, "DELETE", {}, myFunc); // Request to "testpage" with body
ajax(myPage, "GET", {}, myFunc);    // Request to "testpage?{}" with no body

GETing adds the data parameter to the URL, whereas the other functions add it as the request body. Is there a way I can get (no pun intended!) jQuery to send the data in the request body?

Chrome Dev Tools for GET:

GET

Chrome Dev Tools for POST (same for PUT/DELETE):

POST

Anonymous Penguin
  • 1,769
  • 2
  • 30
  • 44
  • it's regular to add the data to the url when you use `GET` method, are you aware of the differences between `GET`, `POST`, `PUT` and `DELETE`? – MaveRick Jul 26 '15 at 02:44
  • Possibly duplicate of http://stackoverflow.com/questions/978061/http-get-with-request-body. I like the guy's conclusion too: "So, yes, you can send a body with GET, and no, it is never useful to do so." – JohnnyFun Jul 26 '15 at 02:45
  • @JohnnyFun I don't see how that question is even "relevant," much less a duplicate. I asked how to do X, and you're trying to close it as a duplicate of something that advises against X? – Anonymous Penguin Jul 26 '15 at 02:49
  • Why would you want to do this? – DarthDerrr Jul 26 '15 at 03:01
  • Look up the definition of GET. Link is totally relevant – charlietfl Jul 26 '15 at 03:17
  • 1
    Thank you, gentlemen. Dude, we're just trying to point you in the right direction. HTTP is a protocol. We programmers avoid violating protocols unless it's absolutely necessary so we can read each other's crap easier. Perhaps the solution is to modify your "ajax" function to use jquery's $.param function if the method is a get and append the result to the url and skip passing it as the request body. Kna mean? – JohnnyFun Jul 26 '15 at 03:59
  • @JohnnyFun the issue is that a URI ignores any notion of character encoding, MIME type, etc. I don't doubt that it's not standard to send GET data in a request body, but it makes no sense that request data is in the body for POST, etc., but in the URI for GET. It adds additional complexity to internals ("if GET then get from URI, else get from body"). – Anonymous Penguin Jul 26 '15 at 19:03

1 Answers1

-4

You can put a body into a GET request as outlined in this SO HTTP GET with request body. But I tried window.fetch and XMLHttpRequest and both don't seem to allow get with a body.

One way you could do it is to proxy the request through your own server and maybe do the request in a server-side language that can send a body in a get request (assuming I'm not missing a way to send a body with a get request with browser javascript...).

So consider putting the data on the querystring instead if you can, since it's weird to put a body into a GET request:

function ajax(url, method, data, success) {
  if (method.toLowerCase() === 'get') {
      data = $.param(data); //url encode the data if GET request
  } else {
      data = JSON.stringify(data); //I think jquery 2.1.4 will pretty much do this for you, btw, but just for good measure, I'll do it how you had it.
  }

  $.ajax({
    contentType: "application/json",
    data: data,
    dataType: "json",
    method: method,
    processData: false,
    url: url
  });
}
JohnnyFun
  • 3,068
  • 2
  • 15
  • 15