简体   繁体   English

无法使用jQuery ajax调用Page Method

[英]Unable to call Page Method using jQuery ajax

I have created a sortable ListView using jQueryUI and now I am stuck in trying to save it back to the database. 我已经使用jQueryUI创建了一个可排序的ListView,但是现在我一直试图将其保存回数据库。 Here's how I have created the ListView: 这是我创建ListView的方法:

<asp:ListView ID="ListViewItems" runat="server">
<LayoutTemplate>
    <div id="sortable1" class="connectedSortable">
        <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
    </div>
</LayoutTemplate>
<ItemTemplate>
    <li class="ui-state-default" id='id_<%# Eval("Item_ID") %>'><%# Eval("Item_Name") %></li>
</ItemTemplate>

There is a button(div) under the ListView which upon being clicked should save the reordered list back to db: ListView下有一个按钮(div),单击该按钮后应将重新排序的列表保存回db:

<div class="btn-info" id="UpdateOrder" onclick="SendUpdatedOrder()">Update Order</div>  

JS File content: JS文件内容:

function SendUpdatedOrder() {
var order = $('#sortable1').sortable('toArray').join(',').replace(/id_/gi, '');
//alert(order);
$.ajax({
    type: "POST",
    url: "~/AdminEditItems.aspx/UpdateRuleOrder",
    data: '{}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    failure: function (response) {
        alert(response.d);
    }
});} 

I have created a page method in AdminEditItems.aspx.cs - 我已经在AdminEditItems.aspx.cs中创建了一个页面方法-

 [WebMethod]
    public static void UpdateRuleOrder(string order)
    {
        ....do something...
    }   

Upon execution the UpdateRuleOrder method is not getting called at all. 执行后,根本不会调用UpdateRuleOrder方法。 Any alerts put by me in UpdateRuleOrder function of JS is getting called successfully but the ajax method somehow is not working. 我在JS的UpdateRuleOrder函数中放置的任何警报都已成功调用,但是ajax方法不起作用。 I am not able to find out where I am going wrong. 我无法找出我要去哪里。

Three things: 三件事:

Remove the tilde and forward slash from your url ie 从网址中删除波浪号和正斜线,即

url: "AdminEditItems.aspx/UpdateRuleOrder",

Add your argument in the data ie 在数据中添加参数

data: '{ "order" : "' + order + '"}',

You also need to EnablePageMethods in your ScriptManager ie 您还需要在ScriptManager EnablePageMethods ,即

<asp:ScriptManager runat="server" EnablePageMethods="true">

Update 更新

I also noticed that you are not returning anything, change your UpdateRuleOrder method to return your object and you will be able to access it like this: 我还注意到您没有返回任何东西,更改UpdateRuleOrder方法以返回您的对象,您将可以像这样访问它:

var myObject = response.d;

As it is within a directory above the url would be: 由于它位于该URL上方的目录中,因此:

url: "../AdminEditItems.aspx/UpdateRuleOrder",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM