想做个测试,但发现此回调函数并没有被调用,为什么?<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/ TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style>
        * {
            margin: 0;
            padding: 0;
        }        body {
            font-size: 12px;
        }        #loading {
            width: 80px;
            height: 20px;
            background: #bbb;
            color: #000;
            display: none;
        }        img {
            border: 0;
            height: 100px;
            width: 100px;
        }        .comment {
            margin-top: 10px;
            padding: 10px;
            border: 1px solid #ccc;
            background: #DDD;
        }        .comment h6 {
            font-weight: 700;
            font-size: 14px;
        }        .para {
            margin-top: 5px;
            text-indent: 2em;
            background: #DDD;
        }
    </style>
    <!--   引入jQuery -->
    <script src="../../js/jquery-1.4.3.js" type="text/javascript"></script>
    <script>
        $(function() {
            //demo1:
            $('#send1').click(function() {
                $("#resText1").empty();
                $.ajax({
                        url:"http://api.flickr.com/services/feeds/photos_public.gne?tags=car&tagmode=any&format=json&jsoncallback=?",
                        beforeSend:function(xhr) {
                            alert('send') ;
                        },
                        success: function(data) {
                            alert('suc');
                            $.each(data.items, function(i, item) {
                                $("<img/> ").attr("src", item.media.m).appendTo("#resText1");
                                if (i == 3) {
                                    return false;
                                }
                            });
                        },
                        complete:function() {
                            alert('complete')
                        },
                        dataType:"json"
                    })
            });
            //demo2:
            $("#send2").click(function() {
                $.post("get1.jsp", {
                    username :  $("#username").val() ,
                    content :  $("#content").val()
                }, function (data, textStatus) {
                    $("#resText2").html(data); // 把返回的数据添加到页面上
                }
                        );
            })
        })
    </script>
</head>
<body>
<br/><div id="loading">加载中...</div><br/>
Demo1:
<br/>
<input type="button" id="send1" value="加载"/><div id="resText1"></div>
<br/>
Demo2:
<br/><form id="form1">
    <p>评论:</p>    <p>姓名: <input type="text" name="username" id="username"/></p>    <p>内容: <textarea name="content" id="content"></textarea></p>    <p><input type="button" id="send2" value="提交"/></p>
</form>
<div class='comment'>已有评论:</div>
<div id="resText2">
</div>
</body>
</html>

解决方案 »

  1.   

    如果指定了script或者jsonp类型,那么当从服务器接收到数据时,实际上是用了<script>标签而不是XMLHttpRequest对象。这种情况下,$.ajax()不再返回一个XMLHttpRequest对象,并且也不会传递事件处理函数,比如beforeSend。 
      

  2.   

    如果楼主希望看到这个beforeSend效果
    就把dataType这个参数略去
      

  3.   

    http://api.flickr.com/services/feeds/photos_public.gne?tags=car&tagmode=any&format=json&jsoncallback=?它获得的是纯json数据啊,不是jsonp。
    ({
    "title": "Recent Uploads tagged car",
    "link": "http://www.flickr.com/photos/tags/car/",
    "description": "",
    "modified": "2010-10-19T02:30:11Z",
    "generator": "http://www.flickr.com/",
    "items": [
       {
    "title": "20970 2010 Fall Classic Car Show Jim Price Chevrolet",
    "link": "http://www.flickr.com/photos/bsabarnowl/5095660330/",
    "media": {"m":"http://farm5.static.flickr.com/4147/5095660330_f28875f8b8_m.jpg"},
    "date_taken": "2010-10-16T14:45:03-08:00",
    "description": " <p><a href=\"http://www.flickr.com/people/bsabarnowl/\">bsabarnowl<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/bsabarnowl/5095660330/\" title=\"20970 2010 Fall Classic Car Show Jim Price Chevrolet\"><img src=\"http://farm5.static.flickr.com/4147/5095660330_f28875f8b8_m.jpg\" width=\"240\" height=\"180\" alt=\"20970 2010 Fall Classic Car Show Jim Price Chevrolet\" /><\/a><\/p> <p>Classic Car Show<br /> Jim Price Chevrolet<br /> Charlottesville, Virginia<br /> October 16, 2010<\/p>",
    "published": "2010-10-19T02:30:11Z",
    "author": "[email protected] (bsabarnowl)",
    "author_id": "10966541@N02",
    "tags": "show classic chevrolet car virginia charlottesville jimprice 20101016"
       }
     
    })
      

  4.   

    不用flickr,everything is ok,原因是jsop协议中,不会创建xmlhttprequest,因此也不会有正常的事件流了。