最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my Ajax request sending "Content-Type: applicationx-www-form-urlencoded" when I have dataT

programmeradmin3浏览0评论

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'
    }
}

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'
    }
}
Share Improve this question edited May 25, 2014 at 16:57 Quentin 944k132 gold badges1.3k silver badges1.4k bronze badges asked May 25, 2014 at 16:52 packetiepacketie 5,06912 gold badges41 silver badges79 bronze badges 4
  • 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 remend 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 Commented May 25, 2014 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 Commented May 25, 2014 at 17:00
  • however he did not send json-formatted data. Therefore the mime type is/was fine. – newBee Commented May 25, 2014 at 17:03
  • As so often, reading documentation 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 Commented May 25, 2014 at 17:32
Add a ment  | 

1 Answer 1

Reset to default 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论