【问题标题】:jQuery not calling the webservicejQuery没有调用网络服务
【发布时间】:2011-02-06 23:28:09
【问题描述】:

我是编程新手,尤其是 jQuery 和 web 服务。我想通过 web 服务将值传递给 to 数据库。

下面是.aspx页面的代码:

     <html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/themes/base/jquery-ui.css"
        rel="stylesheet" type="text/css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/jquery-ui.min.js"
        type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/i18n/jquery-ui-i18n.min.js"
        type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#sortable").sortable();
            $("#sortable").disableSelection();
            $("#sortable input[type=text]").width($("#sortable img").width() - 10);
            $("#sortable label").mouseover(function () {
                $(this).parent().children("input[type=text]").show().val($(this).html());
                $(this).hide();
            });
            $("#sortable input[type=text]").mouseout(function () {
                $(this).parent().children("label").show().html($(this).val());
                $(this).hide();
            });
            $(".ContainerDiv").hover(
                function () {
                    $(this).find(".deleteClass").show();
                },
                function () {
                    $(this).find(".deleteClass").hide();
                });
            $(".deleteClass").click(function () {
                $(this).closest("li").remove();
            });
            $("#orderPhoto").click(function () {
                var photos = $.map($("li.ui-state-default"), function (item, index) {
                    var imgDetail = new Object();
                    imgDetail.Id = $(item).find("img").attr("id");
                    imgDetail.Caption = $(item).find("label").html();
                    imgDetail.Order = index + 1;
                    return imgDetail;
                });
                //photos contains all the photo and order and the chhanged caption.


                //pass to server
                $.ajax({
                    type: "POST",
                    url: "WebService.asmx/updateOrder",
                    data: JSON.stringify(photos),
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        if (data.d === "saved") {
                            $("<p>").text("New order saved!")
                .addClass("success").appendTo("#left");
                        } else {
                            $("<p>").text("Save failed")
                .addClass("failure").appendTo("#left");
                        }
                    }
                });
            });
        });
    </script>
    <style type="text/css">
        #sortable
        {
            list-style-type: none;
            margin: 0;
            padding: 0;
        }
        #sortable li
        {
            position: relative;
            margin: 3px 3px 3px 0;
            padding: 1px;
            float: left;
            text-align: left;
        }
        .deleteClass
        {
            /* PhotoListItem  is relative so relative to it */
            position: absolute;
            top: 1px;
            right: 3px;
            background: black;
            color: Red;
            font-weight: bold;
            font-size: 12px;
            padding: 5px;
            opacity: 0.60;
            filter: alpha(opacity="60");
            margin-top: 3px;
            display: none;
            cursor: pointer;
        }
        .deleteClass:hover
        {
            opacity: 0.90;
            filter: alpha(opacity="90");
        }
        .image_resize {
width: 250px;
height: 250px;
border: 0;
} 
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ListView ID="ListView1" runat="server" GroupItemCount="15">
        <LayoutTemplate>
            <ul id="sortable">
                <li id="groupPlaceholder" runat="server">1</li>
            </ul>
        </LayoutTemplate>
        <GroupTemplate>
            <tr id="itemPlaceholderContainer" runat="server">
                <td id="itemPlaceholder" runat="server">
                </td>
            </tr>
        </GroupTemplate>
        <ItemTemplate>
            <li class="ui-state-default">
                <div class="ContainerDiv">
                    <div class="deleteClass">X</div>
                    <img id='<%#Eval("photo_id")%>' src='<%# "uploads/"+Eval("photo_file_name")%>' alt="" class="image_resize" />
                    <div style="height: 25px; margin-top: 3px">
                        <label>
                            <%# Eval("photo_title")%></label>
                        <input type="text" style="display: none" />
                    </div>
                </div>
            </li>
        </ItemTemplate>
    </asp:ListView>
    <input type="button" id="orderPhoto" value="Save change" />
    </form>
</body>
</html>

我使用了调试工具,可以确认 照片 包含正确的值。它只是调用我不确定的网络服务部分。

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: asp.net vb.net web-services jquery-ui jquery-ui-sortable


    【解决方案1】:

    不要序列化您发送到方法中的数据,它自己会这样做:

    data: photos,
    

    由于 URL 中的文件名没有给出任何数据类型的提示,您可能需要指定它:

    dataType: 'json',
    

    【讨论】:

    • 感谢您的及时回复。我不知道数据的序列化是什么,你的意思是将代码更改为:type:“POST”,url:“WebService.asmx/updateOrder”,data:JSON.stringify(photos),contentType:“application / json; charset =utf-8",
    • 感谢您的及时回复。我不知道数据的序列化是什么,您的意思是将代码更改为:type:“POST”,url:“WebService.asmx/updateOrder”,data:photos,contentType:“json; charset = utf-8”,还有$.ajax 方法如何在 var 照片列表更新后自动运行?抱歉,我可能会问一些愚蠢的问题,这是我第一次使用 web 服务和 javascript。谢谢
    • @Atiq Javed:是的,删除JSON.stringify 调用。您正在使用JSON.stringify 方法对数据进行序列化,但这会以错误的方式对其进行序列化。只需将photos 对象发送到方法中,它就会自行正确序列化它。 $.ajax 调用将在photos 变量创建后运行。那里没有什么奇怪的,它们在代码中一个接一个,所以首先执行一个,然后另一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多