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'];
?>
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:
+3
source to share
2 answers
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 to share