admin管理员组

文章数量:1279021

I am trying to send a post param. to request.php but it returns that the post param. are empty.

<script src=".1.0/jquery.min.js"></script>
$.ajax({
    url: "request.php",
    type: "POST",
    data: "{key:'123', action:'getorders'}",
    contentType: "multipart/form-data",
    plete: alert("plete"),
    success: function(data) {
        alert(data);
    },
    error: alert("error")
});

I am trying to send a post param. to request.php but it returns that the post param. are empty.

<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js"></script>
$.ajax({
    url: "request.php",
    type: "POST",
    data: "{key:'123', action:'getorders'}",
    contentType: "multipart/form-data",
    plete: alert("plete"),
    success: function(data) {
        alert(data);
    },
    error: alert("error")
});
Share Improve this question edited Oct 28, 2017 at 17:36 PaulE asked Aug 11, 2016 at 4:08 PaulEPaulE 832 silver badges9 bronze badges 4
  • try data : {'key':'123', 'action':'getorders'} – Vishnu Commented Aug 11, 2016 at 4:13
  • remove " " from this data format data:"{key:'123', action:'getorders'}" – Jack jdeoel Commented Aug 11, 2016 at 4:13
  • didn't work @DavidJawphan – PaulE Commented Aug 11, 2016 at 17:10
  • @Vishnu same prob.. key is empty – PaulE Commented Aug 11, 2016 at 17:11
Add a ment  | 

5 Answers 5

Reset to default 5

remove " " from this data as data:{key:'123', action:'getorders'}

<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js"></script>
   <script>
     $.ajax({
        url:"request.php",
        type:"POST",
        data:{key:'123', action:'getorders'},
        contentType:"multipart/form-data",
        plete:alert("plete"),
        success:function(data) {
          alert(data);
            },
        error:alert("error")
             }); 

          </script>

You must use FormData for multipart/form-data ,and also need additional option in ajax ..

var request = new FormData();   
request.append('key',123);
request.append('action','getorders');
$.ajax({
    url: "request.php",
    type: "POST",
    data: request,    
    processData : false,
    contentType: false,
    success: function(data) {
        alert(data);
    }    
});

This will help you. You don't want a string, you really want a JS map of key value pairs.

<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
    $.ajax({
       url:"request.php",
       type:"POST",
       data:{key:'123', action:'getorders'},
       contentType:"multipart/form-data",
       plete:alert("plete"),
       success:function(data) {
          alert(data);
       },
       error:function(){
          alert("error");
       }); 

</script>

This should work like a champ , construct object as below and stringify it as JSON.stringify(newObject) then there will be no chance of error

    <script src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script>
       var newObject= new Object();
       newObject.key= '123';
       newObject.action='getorders'
        $.ajax({
           url:"request.php",
           type:"POST",
           data:JSON.stringify(newObject),
           contentType:"multipart/form-data",
           plete:alert("plete"),
           success:function(data) {
              alert(data);
           },
           error:function(){
              alert("error");
           }); 

    </script>

Try this:

data: JSON.stringify({key: '123', action: 'getorders'}),
contentType: "application/json"

本文标签: JavaScript jQuery AJAX POST data errorStack Overflow