JavaScript jQuery AJAX POST error

I am trying to send post param. before request.php , but returns the value of 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") }); 
+5
source share
5 answers

You should use FormData for multipart/form-data , and also use the extra 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); } }); 
+3
source

remove the "" 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> 
+4
source

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

 <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> 
+1
source

This should work like a champion, build an object as shown below, and stringify it as JSON.stringify(newObject) , then there will be no errors

  <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
source

Try the following:

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

All Articles