当我使用函数参数传递数字以使用ajax发送时它正常工作但是当我传递字符串如下面给出的它没有在php页面中接收时。
function loadState(country) {
$.ajax({
url: 'cardstate.php',
type: 'get',
data: { 'country': country },
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function(data) {
alert(data);
}
});
}
loadState("Pakistan")
// cardstate.php:
$country = $_GET['country'];
echo $country; // not receving string while when i put number its receiving
答案 0 :(得分:0)
在你的cardstate.php中你应该使用json_encode
// cardstate.php:
$country = $_GET['country'];
echo json_encode($country); // not receving string while when i put number its receiving
答案 1 :(得分:0)
function loadState(country) {
$.ajax({
url: 'cardstate.php',
type: 'GET',
data: 'country='+country, // <--- changed this as its GET
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function(data) {
alert(data.country); // <---- made this change
}
});
}
loadState("Pakistan")
在PHP中,使用json_encode()
// cardstate.php:
$country = $_GET['country'];
echo json_encode(array('country' => $country));