简体   繁体   中英

Sending data using ajax to php file not working

I'm trying to send data from the fields to a PHP file, but it always returns null.

Is my approach correct, or I'm missing something?

Thank in advance. my ajax


$.ajax({
url: '/GetData.php',
dataType: 'json',
type: 'POST',
data: {
a:a,
b:b,
c:c
},
contentType: 'application/json',
success: function(response){
console.log('success '+ JSON.stringify(response));
search_table =  JSON.stringify(response)

$('#mytable').html(search_table);

},
error: function(err){
console.log('error '+JSON.stringify(err));
//alert(JSON.stringify(err))
}
});


my GetData.php file

<?php
 $a = $_GET["a"];
 $b = $_GET["b"];
 $c = $_GET["c"];

$remote_url = "http://mydomain/GetDataDetails/?name=".$a."&age=".$b."&degree=".$c;
$opts = array(
'http'=>array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method'=>"GET",
)
);
$context = stream_context_create($opts);
$out=json_decode(file_get_contents($remote_url, false, $context),true);
header('Content-Type: application/json');
echo json_encode($out);
?>

Answer from comment:

  • Change from $_GET to $_POST in PHP
  • Remove contentType: 'application/json', from the Ajax request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM