我正在尝试一些非常基本的东西,它不起作用:(我得到警报'测试',就是这样:()
<script name="text/javascript">
function myFunction(part_id, product_id, type)
{
alert('test');
$.ajax({
type: 'post',
url: '2.php',
data: {lname: "www", name: "Natalie"},
complete: function (txt) {
alert("complete");
alert(txt);
}
});
}
</script>
在2.php我只有一行(在同一目录下):
echo "test has been run";
Ajax将返回对象数据:
Object {readyState: 4, responseText: "test has been run", status: 200, statusText: "OK"}
你必须从那个对象获得响应文本。
function myFunction(part_id, product_id, type)
{
alert('test');
$.ajax({
type: 'post',
url: '2.php',
dataType:'text',
data: {lname: "www", name: "Natalie"},
complete: function (txt) {
console.log(txt);
alert("complete");
alert(txt.responseText);
}
});
}
使用success
事件。它将得到php的结果。
查看success() &完成()
<script name="text/javascript">
function myFunction(part_id, product_id, type)
{
alert('test');
$.ajax({
type: 'post',
url: '2.php',
data: {lname: "www", name: "Natalie"},
complete: function (txt) {
//do something
},
success: function (result) {
alert("success");
alert(result);//test has been run
}
});
}
</script>