duanjitong7226 2011-05-25 00:34
浏览 28
已采纳

无法让jQuery ajax工作

I am trying to convert my webpage to ajax but I cant get it to return anything.

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>test page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
$(document).ready(function(){   
  // events
$("#test").click(function(){
    var myData;
    myData = makeDataVar();
    sendData(myData);
});     
  // functions
function makeDataVar(){
    return "name=joe&location=bostin";
}
function sendData(myData){
    alert("start mydata function");
    $.ajax({
        type: "POST",
        url: "http://www.mypage.com/submitMe.php",
        dataType : "json",
        data: myData,
        success: function (msg) {
            alert("Data saved: " + nameMsg);
            alert("Data saved: " + locationMsg);
            alert("data saved: " + error);
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert("error " + errorThrown);
        }
    }); 
    alert("end mydata function");           
}
}); // end document.ready
//-->
</script>    
</head>

<body>

<button type="button" id="test" >Add New</button>

</body>
</html>

PHP

<?php
$myVar = $_POST['name'];
$return['nameMsg'] = "thanks $myVar";
$myVar = $_POST['location'];
$return['locationMsg'] = "from $myVar";
$return['error'] = false;
echo json_encode($return);
?>

I am getting all of my javascript alerts except for those inside the ajax statement (neither success or error). It is almost like my script is skipping over that one statement. I am not sure what I am missing here but I have spent a couple of hours trying to figure it out. Any help you could offer would be greatly appreciated.

  • 写回答

5条回答 默认 最新

  • dongyan5641 2011-05-25 00:46
    关注

    My vote is that your variables inside the success callback are undefined, so the method doesn't compile or execute.

    alert("Data saved: " + nameMsg);
    alert("Data saved: " + locationMsg);
    alert("data saved: " + error);
    

    nameMsg, locationMsg, and error don't appear to be defined anywhere. This seems to masquerade as a method not getting executed sometimes, but Firebug or Chrome's developer tools should show you the error.

    Does your success callback get executed if you don't try to use them? Just do an

    alert("foo");
    

    and see if that works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启