jquery ajax document,javascript - Run ajax request on document.ready jquery - Stack Overflow

Ajax

$(document).ready(function() {

$.ajax({

type: 'POST',

url: '../include/ListOfCities.php',

dataType: "json",

data: {

Country: "Japan"

},

success: function(data) {

console.log(data);

var city = ('#city');

$(city).empty();

for (var i = 0; i < data.length; i++) {

$(city).append('' + data[i].city_name + '');

}

}

});

});

php

$country = mysql_real_escape_string($_POST['Country']);

$stmt = $dbh->prepare("SELECT * FROM city_tbl WHERE country_name = ? ");

$stmt->bindValue(1, $country, PDO::PARAM_STR);

if ($stmt->execute()) {

if ($stmt->rowCount() > 0) {

while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {

$citylist[] = array('sysid' => $selected_row['sys_id'], 'code' => $selected_row['city_code'], 'name' => $selected_row['city_name'], 'parentid' => $selected_row['parent_id']);

}

$input = array_map("unserialize", array_unique(array_map("serialize", $citylist)));

echo json_encode($input, JSON_UNESCAPED_UNICODE);

}

}

I want to display the all the city in japan in a select option menu i want to load the cities when the page loads i am sending the ajax request like above but i don't get any result the ajax above works fine when i use it on button. Is sending ajax request different from on button click and on document ready..Any suggestion how to make ajax request on document ready is appreciated

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值