Can't generate $ _POST with $ .ajax function

I am trying to host my site on the internet, but I have a problem with ajax, so I tried to create a simple function to test it:
This is my jQuery function:

function test(){
    $.ajax({
        async:true,
        type:"POST",
        url:"controller/test.php",
        data:"order=testingConnectionToServer",
        success:function(result){
                alert(result);
        },
        error:function(xhr,msgError){
            alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
            alert("responseText: "+xhr.responseText);
        }
    }); 

}

      

I am calling this function when the page is loaded.
And this is my php code in test.php

:

<?php
echo 'test ok: $_POST[\'order\'] = '.$_POST['order'];
?>

      

But this is the result I get:
enter image description here

Note that this feature works on localhost, but when I put it on the internet it doesn't work! This is what the console shows:
enter image description here

+3


source to share


2 answers


I think you have a call $.ajaxSetup

that overrides the default contentType

.

Send the correct one contentType

to your challenge $.ajax

.



It's application/x-www-form-urlencoded

for the format var=1&var2=2

.

+2


source


You need to put curly braces {} inside the data object. Take the updated code. I think this one will work for you. If you are returning json or any other exchange format, you need to define the data type.



function test(){
$.ajax({
    async:true,
    type:"POST",
    dataType : 'json',
    url:"controller/test.php",
    data:{order : 'Your text'},
    success:function(result){
            alert(result);
    },
    error:function(xhr,msgError){
        alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
        alert("responseText: "+xhr.responseText);
    }
}); 

 }

      

0


source







All Articles