5

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.com/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",
    complete: alert("complete"),
    success: function(data) {
        alert(data);
    },
    error: alert("error")
});
PaulE
  • 83
  • 2
  • 9

5 Answers5

5

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

<script src="https://ajax.googleapis.com/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",
        complete:alert("complete"),
        success:function(data) {
          alert(data);
            },
        error:alert("error")
             }); 

          </script>
Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
3

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);
    }    
});
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
1

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.com/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",
       complete:alert("complete"),
       success:function(data) {
          alert(data);
       },
       error:function(){
          alert("error");
       }); 

</script>
Gopinath Shiva
  • 3,822
  • 5
  • 25
  • 48
Sinscary
  • 652
  • 12
  • 32
0

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.com/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",
           complete:alert("complete"),
           success:function(data) {
              alert(data);
           },
           error:function(){
              alert("error");
           }); 

    </script>
0

Try this:

data: JSON.stringify({key: '123', action: 'getorders'}),
contentType: "application/json"
Rahul Malu
  • 556
  • 2
  • 9
  • tried it... it return nothing.. even the "key is empty" – PaulE Aug 11 '16 at 17:07
  • Try to verify the url. Also share the error received. – Rahul Malu Aug 12 '16 at 06:14
  • $.ajax({ url: "request.php", type: "POST", data: '{ "key":"123", "action": "getorders"}', contentType: "application/json", complete: alert("complete"), success: function(data) { alert(data); }, error: function(jqXHR, textStatus, errorThrown) { console.log(textStatus, errorThrown); } }); Please note: In JSON - Strings (Key names and string values etc) are present in double quotes (" ") and not in single quote. – Rahul Malu Aug 12 '16 at 06:18