今天我使用的是 j Query $.post();方法,但我找不到我遇到的问题的原因。代码片段如下
$(document).ready(function(){
//When the button 1 is clicked
$('#generateTable1').click(function(){
//Get the Json data of the product low in stock
$.post('database_to_phpJSON.php',{option:1},function(value){
value=JSON.parse(value);
console.log(value);
alert("hi");
},{dataType:'json'});
});
});
现在,当我运行代码时,我没有收到警报消息“Hi”,并且在 firebug 的控制台中我没有看到 console.log 输出。 但是当我从代码中删除 dataType 时,一切都正常...请让我知道原因。
最佳答案
这是因为在使用 $.post
时,您仅将 dataType 作为字符串传递
$.post('database_to_phpJSON.php', {option:1}, function(value){
console.log(value);
alert("hi");
}, 'json').fail(function() {
console.log(arguments); // will tell you what's wrong
});
当使用正确的数据类型时,jQuery 将自动解析结果
关于javascript - $.post() 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22281459/