【问题标题】:jquery ajax request firebug errorjquery ajax请求萤火虫错误
【发布时间】:2010-09-25 16:50:01
【问题描述】:

我正在使用 php/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();

    });

});

短信.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' 设置为 true 时,会显示以下结果 -

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 这是我的基本网址

【问题讨论】:

  • 找不到sms.php。所以你的 JavaScript 中的 URL 是错误的,它和你的 HTML 页面在同一个文件夹中吗?

标签: php jquery firebug


【解决方案1】:

您可能想尝试将 e.preventDefault() 语句放在 $.ajax 调用之前。

编辑:

我的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 表单处理代码。 “消息已成功发送”。在输入字段正下方以绿色显示。

【讨论】:

  • 是的。不是根本原因:) 在我的情况下,它允许 AJAX 调用完成,而无需刷新整个页面。您的 jquery.min.js 文件在哪里托管?
【解决方案2】:

当在 Ajax 调用中 this 是指您需要执行此操作的 Ajax 对象时

var __this = this; 

在进入 Ajax 调用之前,应该是

data    : __this.serialize()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    相关资源
    最近更新 更多