2010-09-23 123 views
1

我使用php/ajax提交表单而不刷新页面。这里是我的文件 -jquery ajax请求萤火虫错误

coupon.js

jQuery(document).ready(function(){ 
     jQuery(".appnitro").submit(function(e) { 
$.ajax({ 
      url  : "http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php", 
      type : "post", 
      dataType: "json", 
      data : $(this).serialize(), 
      success : function(data) { 
         for(var id in data) { 
          jQuery('#' + id).html(data[id]); 
         } 
         } 

     }); 
//return false or 
e.preventDefault(); 

    }); 

}); 

sms.php

<?php 
    //process form 
$res = "Message successfully delivered"; 
    $arr = array('mess' => $res); 
    echo json_encode($arr);//end sms processing 
    unset ($_POST); 
    ?> 

这里是代码为我的html页面 -

<form id="smsform" class="appnitro" action="http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php" method="post"> 
... 
</form> 
<div id="mess" style="background:green;"></div> 

现在,当我点击提交按钮没有任何反应,萤火虫显示下面的控制台面板 -

POST http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php 

404 Not Found 1.29s `jquery.min.js (line 130)` 

Response 

Firebug needs to POST to the server to get this information for url: 
http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php 

This second POST can interfere with some sites. If you want to send the POST again, open a new tab in Firefox, use URL 'about:config', set boolean value 'extensions.firebug.allowDoublePost' to true 
This value is reset every time you restart Firefox This problem will disappear when https://bugzilla.mozilla.org/show_bug.cgi?id=430155 is shipped 

当我设置“extensions.firebug.allowDoublePost”来然后按照真实的结果显示 -

POST http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php 

404 Not Found 1.29s `jquery.min.js (line 130)` 

Response - 

{"mess":"Message successfully delivered"} 

谁能帮助我在固定的404没有发现这一错误的萤火。为什么它会显示jquery.min.js (line 130)

P.S - 不要担心http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street这是我的基本URL

+0

它无法找到sms.php。所以你的JavaScript中的URL是错误的,它是否与HTML页面上的表单位于同一个文件夹中? – 2010-09-23 06:56:38

回答

0

你可能想尝试把e.preventDefault()语句中的$就调用之前。

编辑:

我x.html,对应于你的HTML页面


<!DOCTYPE html> 
<html> 
<head> 
<title>x</title> 
<script 
    type="text/javascript" 
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> 
</script> 
<script type="text/javascript" src="/so/x.js"></script> 
</head> 
<body> 
<form id="smsform" class="appnitro" action="/so/x.php"> 
<input type="text" name="zz"> 
<input type="submit"> 
</form> 
<div id="mess" style="background:green;"></div> 
</body> 
</html> 

我x.js,对应于您coupon.js


jQuery(document).ready(function(){ 
    jQuery(".appnitro").submit(function(e) { 
    e.preventDefault(); 
    $.ajax({ 
     url  : "/so/x.php", 
     type : "post", 
     dataType: "json", 
     data : $(this).serialize(), 
     success : function(data) { 
     for(var id in data) { 
      jQuery('#' + id).html(data[id]); 
     } 
     } 

    }); 
    //return false or 
    //e.preventDefault(); 
    }); 
}); 

我的X。 php,对应你的sms.php


<?php 
$res = "Message successfully delivered."; 
$arr = array('mess'=>$res); 
echo json_encode($arr); 
unset($_POST); 
?> 

这实际上适用于我的环境,虽然我没有剩下的HTML标记或额外的PHP表单处理代码。 “邮件已成功发送”。在输入字段正下方以绿色显示。

+0

@jt:no.does没有帮助 – ayush 2010-09-23 07:26:57

+0

是的。没有根本原因:)在我的情况下,它允许AJAX调用完成,而不必刷新整个页面。你的jquery.min.js文件被托管在哪里? – JTP 2010-09-23 14:12:14

0

当里面的AJAX调用这指的是Ajax对象,你需要做到这一点

var __this = this; 

再进Ajax调用,那么这将是

data : __this.serialize() 

或查找使用Google的Ajax调用中的上下文。或者在进入Ajax调用之前将数据串入到变量中。