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")
});
4
  • try data : {'key':'123', 'action':'getorders'} Commented Aug 11, 2016 at 4:13
  • remove " " from this data format data:"{key:'123', action:'getorders'}" Commented Aug 11, 2016 at 4:13
  • didn't work @DavidJawphan Commented Aug 11, 2016 at 17:10
  • @Vishnu same prob.. key is empty Commented Aug 11, 2016 at 17:11

5 Answers 5

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>
Sign up to request clarification or add additional context in comments.

1 Comment

you can false contentType as you want then it should work
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);
    }    
});

Comments

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>

Comments

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>

1 Comment

still key is empty
0

Try this:

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

3 Comments

tried it... it return nothing.. even the "key is empty"
Try to verify the url. Also share the error received.
$.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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.