doujia4759 2013-05-26 12:58
浏览 71

AJAX不会运行ajaxStart或加载脚本

So my code looks like this http://jsfiddle.net/qBZMh/4/

Now, on this it won't add the class "active" because it can't find the requested file, for me it finds the file but it won't run the file. My PHP-file look like this:

<?php
$time = time();
mysql_query("INSERT INTO friends
(userid, userid2, `type`, `date`) VALUES
('1', '2', '1', '$time' )") or die(mysql_error());
?>

jQuery:

$(document).ready(function () {
    $("#addfriend").click(function () {
        $.ajax({
            url: "snippets/profile_addfriend.php",
            context: document.body
        }).done(function () {
            $("#addfriend").addClass("active");
        });
    });
});

$(document).ready(function () {
    $("body").on({
        ajaxStart: function () {
            $(this).addClass("loading");
        },
        ajaxStop: function () {
            //$(this).removeClass("loading");
        }
    });
});

There should be variables instead of numbers but just to see if it actually runs I have removed that.

So, the problems are:

  1. It finds the PHP-file but it won't run the script.

  2. It doesn't show a loading bar, I might have understood this wrong but I want it so my button gets replaced by the loading bar and then appears again when the script have been executed.

  • 写回答

2条回答 默认 最新

  • dou47732 2013-05-26 13:06
    关注

    You have no fallback for a 404. If it cannot find it, it will prompt a 404 not found error. you could add

       error: function(XMLHttpRequest, textStatus, errorThrown){
        alert('status:' + XMLHttpRequest.status + ', status text: ' + XMLHttpRequest.statusText);
    },
    

    to add a catch for a 404.

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教