ajax代码没有在后台导航我到url

时间:2012-11-01 03:26:49

标签: javascript jquery ajax

我正在尝试使用ajax在后台导航到php脚本,以便它执行一些命令,但问题是它没有执行命令,这告诉我我的ajax不正确或我做错了不称之为ajax的东西。你能不能看看代码,说明我做错了什么?我知道php脚本假设导航到insertQuestion.php确实有效,因为如果我使用表单操作导航到该页面,那么它会执行该脚本中的命令,但是当我尝试使用ajax导航到脚本时在后台,它只是不这样做:

代码:

<form id="QandA" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return myClickHandler()">

....

</form>

 function myClickHandler()
    {
        if (!validation())
            return false;

        if (!confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" ))
            return false;

        $.ajax({
            url: "insertQuestion.php",
            data: $("#QandA").serialize(),
            async: false
        });

        return true;
    }

更新:

它仍然没有导航到insertQuestion.php页面,现在的问题是它没有检查验证并且没有在javascript函数中显示确认框。下面我列出了当前代码现在的更新:

<form id="QandA" action="<?php echo htmlentities($action); ?>" method="post" onsubmit="return myClickHandler(); return false;">

....

</form>

         <script type="text/javascript">

    function myClickHandler()
    {
        if (!validation())
            return false;

        if (!confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" ))
            return false;

       $.ajax({
            url: "insertQuestion.php",
            data: $("#QandA").serialize(),
            async: false
            type: "POST"
        });

        return true;
    }

</script>

2 个答案:

答案 0 :(得分:0)

使用类型:“POST”因为

  

type String默认值:'GET'要发出的请求类型(“POST”或   “GET”),默认为“GET”。注意:其他HTTP请求方法,例如   PUT和DELETE也可以在这里使用,但它们不受支持   所有浏览器。

.ajax

$.ajax({
            url: "insertQuestion.php",
            data: $("#QandA").serialize(),
            async: false,
            type: "POST"
        });

关于提交: HTML:

<form id="QandA">
...
</form>

JS:

$(function() {
myClickHandler=function(e)
    {
        if (!validation())
            return false;

        if (!confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards Questions, Options and Answers for your Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n" ))
            return false;

        $.ajax({
            url: "insertQuestion.php",
            data: $("#QandA").serialize(),
            async: false,
            type: "POST"
        });

        return true;
    };



$('#QandA').submit(myClickHandler);



});

答案 1 :(得分:-1)

请删除htmlentities()

<form id="QandA" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return myClickHandler()">

....

</form>

或者通过添加“return false”尝试其他方式提交。

<form id="QandA" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return myClickHandler(); return false;">

....

</form>