$_POST is not working in ajax form submit?

后端 未结 11 1568
遥遥无期
遥遥无期 2021-01-17 12:57

When I try to check user input name is already exist by ajax form submit !But it only get Undefined index: username in sessions.php ,what is missin

11条回答
  •  礼貌的吻别
    2021-01-17 13:40

    There are few issues with your code, such as:

    • ... it only get Undefined index: username in sessions.php

      The problem is because of the following two lines,

      contentType : false,
      processData: false,
      

      From the documentation,

      contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')
      Type: Boolean or String
      When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header.

      and

      processData (default: true)
      Type: Boolean
      By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

      Hence, $_POST array would be empty in sessions.php page if you set contentType and processData to false, and that's why you're getting this undefined index: username error. But having said that, since you're sending a file with your AJAX request, it's okay to set these settings as false, which is further explained in the following point.

    • .serialize() method creates a URL encoded text string by serializing form control values, such as ,