ajax 请求函数在调用时不起作用


ajax request function does not work when its called

  <script type="text/javascript"src="prototype.js"></script>
<script type="text/javascript">
//<![CDATA[
document.observe("dom:loaded", function() {
    function sendRequest() {
        var oform = document.forms[0];
        var sBody = getRequestBody(oform);
        var oOptions = {
            method: "post",
            parameters: sBody,
            onSuccess: function (oXHR, oJson) {
                saveResult(oXHR.responseText);
            },
            onFailure: function (oXHR, oJson) {
                saveResult("An error occurred: " + oXHR.statusText);
            }
        };
        var oRequest = new Ajax.Request("edit_status.php", oOptions);      
    }
    function saveResult(sMessage) {
        var divStatus = document.getElementById("divStatus");
        divStatus.innerHTML = "Request completed: " + sMessage;            
    }

    });
//]]>
</script>

我是 ajax 的新手。我手头有一个项目,确实需要很多 ajax 功能。 我正在遵循我购买的一本书中的上述代码。当我在本地服务器上复制此代码时,当我单击"提交"按钮时,ajax.request 函数不起作用。它直接将我带到php页面。请有人帮我看看这个吗?

**

<form method="post" action="SaveCustomer.php" 
      onsubmit="sendRequest(); return false">
<p>Enter customer information to be saved:</p>
<p>Customer Name: <input type="text" name="txtName" value="" /><br />
Address: <input type="text" name="txtAddress" value="" /><br />
City: <input type="text" name="txtCity" value="" /><br />
State: <input type="text" name="txtState" value="" /><br />
Zip Code: <input type="text" name="txtZipCode" value="" /><br />
Phone: <input type="text" name="txtPhone" value="" /><br />
E-mail: <input type="text" name="txtEmail" value="" /></p>

</form>
<div id="divStatus"></div>

**

**

header("Content-Type: text/plain");
//get information
$sName = $_POST["txtName"];
$sAddress = $_POST["txtAddress"];
$sCity = $_POST["txtCity"];
$sState = $_POST["txtState"];
$sZipCode = $_POST["txtZipCode"];
$sPhone = $_POST["txtPhone"];
$sEmail = $_POST["txtEmail"];
//status message
$sStatus = "";
//database information
$sDBServer = "localhost";
$sDBName = "ajax";
$sDBUsername = "root";
$sDBPassword = "";

//create the SQL query string
$sSQL = "Insert into Customers(Name,Address,City,State,Zip,Phone,`Email`) ".
          " values ('$sName','$sAddress','$sCity','$sState', '$sZipCode'".
          ", '$sPhone', '$sEmail')";
$oLink = mysql_connect($sDBServer,$sDBUsername,$sDBPassword);
@mysql_select_db($sDBName) or $sStatus = "Unable to open database";
if ($sStatus == "") {
    if(mysql_query($sSQL)) {
        $sStatus = "Added customer; customer ID is ".mysql_insert_id();
     } else {
        $sStatus = "An error occurred while inserting; customer not saved.";
    }
}
mysql_close($oLink);
echo $sStatus;

?>**

你没有触发ajax,我看到你定义了选项,但就是这样尝试使用jQuery,您可以等待表单提交

$('your form').on('submit', function(event){
    event.preventDefault();
    $.ajax({
        url:'your url',
        type:'post',
        data:'your data',
        success:function(data, jxhr){
            //your success function
        },
        error:function(){}
    });
});

e.preventDefault() 阻止同步提交触发默认方法

查看您的代码,可以将 sendRequest() 更改为 sendRequest(event),然后添加 event.preventDefault。我总是有返回错误的问题