9

I'm using the jQuery Post function, for example:

var fooVar = true;
var barVar = 1;
var bazVar = "baz";

$.post("url",
   {
        foo: fooVar,
        bar: barVar,
        baz: bazVar
   },
   function(){
     alert("success");
   }
);

In my logs, I'm seeing an intermittent issue where requests to "url" are being made without any form parameters, and I only have this one function which calls it.

Is there ever a situation in which the POST request can be fired, without sending the form parameters specified in jQuery Post?


I would expect to see:

foo=true&bar=1&baz=baz

However there are no form parameters at all:


UPDATE: This issue seems to be mainly on Internet Explorer browsers (IE7-IE11) from looking at the stats, however its not exclusive to IE (Chrome, Firefox have also had issues).

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • Do the headers have the form and the values are blank or it's not even in the request..? – Drewness Feb 20 '14 at 14:11
  • @Drewness It's not even in the request! The header has no form parameters at all. – Curtis Feb 20 '14 at 14:12
  • can you show the server side script in which you are processing post data? – ADAD.TJ Feb 20 '14 at 14:14
  • @Curt And you're not using an `$.ajaxSetup()` else where, right? – Drewness Feb 20 '14 at 14:14
  • 1
    @ADAD.TJ This is not a server-side issue. I'm looking at the request logs stored on server, and there's no form parameters. This issue is intermittent as well. – Curtis Feb 20 '14 at 14:15
  • @Drewness That's correct, no use of ajaxSetup. – Curtis Feb 20 '14 at 14:16
  • I had some troubles with the data send in some cases, did you try to get all the second parameter between double quotes? $.post("url"," { foo: fooVar, bar: barVar, baz: bazVar }", ..... – seba47 Feb 20 '14 at 14:20
  • @seba47 - Encapsulating all of the variables in double quotes does not make it valid JSON. – Drewness Feb 20 '14 at 14:23
  • 1
    When you say the issue is "intermittent" do you mean it works sometimes and not others? Is there any pattern to this? Perhaps different client browsers, or something? – musefan Feb 20 '14 at 14:36
  • @musefan That's right, I haven't got stats yet, but at the moment it appears to be spread across all browsers. I'm baffled though as I wouldn't expect this behavior! – Curtis Feb 20 '14 at 14:38
  • What method are you using to check the request to see if the parameters are there? Any particular software? – musefan Feb 20 '14 at 14:40
  • Sometimes I am getting some pretty strange behavior like this when running the IE Developer Helper (F12) while debugging against Visual Studio – Fiffe Apr 11 '14 at 13:52
  • Maybe you should add some information about the type / value of variables your are trying to send to the server. – Holt Apr 11 '14 at 14:07
  • @Holt Thanks, I've updated my question to show an example of the types of data I'm sending – Curtis Apr 11 '14 at 14:13
  • Are u sure that requests in your logs was all from one client? may be some robots here? – vp_arth Apr 11 '14 at 14:15
  • @musefan I'm looking at session logs which show the requests made with the empty form parameters – Curtis Apr 11 '14 at 14:15
  • @vp_arth I'm confident this is from different real users – Curtis Apr 11 '14 at 14:16
  • 1
    It can be some kind of sabotage) Each your user can to send to you different queries, and even can to edit your js code.. You should try to repeat this behavior for yourself – vp_arth Apr 11 '14 at 14:18
  • you can just send a nonce to make sure this comes really from your form –  Apr 12 '14 at 21:26
  • I have had a similar issue before, and after a lot of investigation I found it to be WWW-Authenticate requests before the actual POST request were fired, is it possible to include the raw request and response headers? – epoch Apr 14 '14 at 13:26

4 Answers4

2

jQuery Post can send a request without form parameters when the parameter values are undefined.

For example if we have the following:

var fooVar = undefined;
var barVar = 1;
var bazVar = "baz";

$.post("url",
   {
        foo: fooVar,
        bar: barVar,
        baz: bazVar
   },
   function(){
     alert("success");
   }
);

Then the form parameters posted will be:

bar=1&baz=baz

Now this doesn't solve my actual issue (from what I can tell correct conditions have been put in place to only make the call if all variables have a value), but it does answer my question.

Curtis
  • 101,612
  • 66
  • 270
  • 352
2

Sounds like a browser version specific issue, try to reproduce it locally with different versions of IE. It might be a typo in the code that is handled gracefully by some versions of IE but not the others (like the trailing comma in arrays) - run a JSLint/JSHint on your JavaScript code. Another scenario I can think of is CORS preflight OPTIONS request - that has no body. Are you sure that you are not executing a CORS request? Does your Ajax URL match the origin?

peterfoldi
  • 7,451
  • 5
  • 21
  • 19
1

Instead of using the $.post shorthand, try using $.ajax instead; not sure whether that will solve it, but it certainly won't hurt to try it out.

Plus, you'll have one less function call to worry about. Micro-optimisations ftw!

$.ajax({
  type: "POST",
  url: "url",
  data: { foo: bar }
});
Lino Silva
  • 471
  • 4
  • 8
0

$.post is a shorthand way of using $.ajax for POST requests, so there isn't a great deal of difference between using the two . maybe the problem is somewhere else in your code, not in jquery or you browser.

but try $.ajax. it is generally better to use if you require a greater depth of configuration over your ajax request. it should work

e.g.

$.ajax({
  type: "POST",
  url: "test_url",
  data: { name: "John", location: "Boston" },
  success: function(response) {
     alert('success !');
  }
});

here is more https://api.jquery.com/jQuery.ajax/

ikos23
  • 4,879
  • 10
  • 41
  • 60