4

When I am using the following to respond to a button click, it it called (verified by using the console.log()), however, the http request it generated has the header "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n". Shouldn't it be json?

I am using google chrome 34.0.1847.132 on Ubuntu. Jquery version 1.8.3.

Thanks in advance!

function action (mode) {
    console.log("action called with mode " + mode);
    $.ajax({
      type: "POST",
      url: '/saas.php',
      data: {
          action: (mode == 1)? "start" : "stop"
      },
      dataType: "json",
      success: function(data) {
          //alert(data);
          if (data.msg != null) {
              alert(data.msg);
          } else {
            if (mode == 1) {
                document.getElementById('createLoadGen').innerHTML = 'creating loadGen...';
                setTimeout(checkStatus, 1000);
            }
          }
      }
    });
    if (mode == 2) {
        document.getElementById('createLoadGen').innerHTML = '<button onclick="action(1)" >create LoadGen</button>';
        document.getElementById('deleteLoadGen').style.display = 'none'
    }
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
packetie
  • 4,839
  • 8
  • 37
  • 72
  • you are mixing up two things. the application/x-form is just the MIME-Type. It's like the used standard to send data. Json is a data format which is send and wrapped in a mime type. Addtionally i recommend you to check the jquery manual. dataType does not define how the data is send. It just tells jquery that you expect a JSON-String as answer. – newBee May 25 '14 at 16:58
  • 1
    @newBee — Err. `application/x-www-form-urlencoded` and `application/json` are different MIME types used to describe different types of content. It is rarely sensible to send JSON wrapped in URL encoded data. – Quentin May 25 '14 at 17:00
  • however he did not send json-formatted data. Therefore the mime type is/was fine. – newBee May 25 '14 at 17:03
  • As so often, reading [documentation](https://api.jquery.com/jquery.ajax/) actually helps: *"`dataType`: The type of data that you're expecting back **from the server**. If none is specified, jQuery will try to infer it based on the MIME type of the response [...]"* – Felix Kling May 25 '14 at 17:32

1 Answers1

11

Not generally. The Content-Type header in the request describes the content in the request body, not the type of content that is expected in the response (describing the expected response format is the job of the Accept header).

By default, jQuery will encode the data using the standard encoding used by HTML forms.

Now, it might be that the server is expecting the request to be formatted as JSON, in which case you would need to make the following modifications to the jQuery call:

  1. Say that you are sending JSON
  2. Encode the content you are sending as JSON instead of letting jQuery encode it itself

Such:

  data: JSON.stringify({
      action: (mode == 1)? "start" : "stop"
  }),
  contentType: "application/json",

sass.php will then have to parse the JSON request instead of letting PHP do it invisibly in the background and just plucking the data out of $_POST.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks @Quentin and others for commenting and answering this question! It really helps. – packetie May 25 '14 at 17:39
  • Thanks for explaining this. Been looking for an example of setting the properties of the AJAX syntax, and your example happens to show what exactly should the format syntax be to pass into `JSON.stringify()` then pass to the AJAX. Other examples I found online would wrap the contents for `JSON.stringify()` with single and double quotes, and I would always get errors for converting `System.String` to `Generic.IDictionary.` – tom_mai78101 Jan 30 '17 at 15:41