在asp.net 4.5 Web窗体站点中使用jQuery AJAX调用PageMethod

时间:2014-03-18 05:08:08

标签: ajax call

Aspx

<%@  Item Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            debugger;
            source =
        {
            datatype: "json",
            datafields: [
                                { name: 'ItemID' },
                                { name: 'ItemName' }
            ],
            id: 'ItemURL',
            async: false
        };

            var itemID = '1';
            var itemName = 'food';
            $.ajax({
                type: 'POST',
                data: "{ itemID: '" + itemID + "', itemName:'" + itemName + "'}",
                url: 'Default.aspx/GetAllItems',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (data) {
                    var favoriteItems = new Array();
                    debugger;
                    for (var i = 0; i < (data.d.length) ; i++) {
                        var row = {};
                        row["ItemID"] = data.d[i].ItemID;
                        row["ItemName"] = data.d[i].ItemName != null && data.d[i].ItemName != undefined ? data.d[i].ItemName : '';
                        favoriteItems[i] = row;
                    }
                    source.localdata = favoriteItems;
                    var dataAdapter = new $.jqx.dataAdapter(source);
                    dataAdapter.dataBind();
                },
                error:
          function (XMLHttpRequest, textStatus, errorThrown) {
              debugger;
          }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnableItemMethods="True" />
        <div>
        </div>
    </form>
</body>
</html>

cs页

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string GetAllItems(String itemID, String itemName)
    {
        return "Page Method";
    }
}

以上是我创建的代码。

我面临的问题是没有调用webmethod(GetAllItems),但是在ajax调用中,执行成功块并将错误返回为“Authentication failed”。

我也包括我的web.config

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-tcmversion3-d068790f-cbfb-40cb-b881-756e476a0411;AttachDbFilename=|DataDirectory|\aspnet-tcmversion3-d068790f-cbfb-40cb-b881-756e476a0411.mdf;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization" />
        <add namespace="Microsoft.AspNet.Identity" />
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
    <membership>
      <providers>
        <clear />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear />
      </providers>
    </profile>
    <roleManager>
      <providers>
        <clear />
      </providers>
    </roleManager>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.web.extensions>
    <scripting>
      <webServices>
        <authenticationService enabled="true" />
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

0 个答案:

没有答案