2016-09-28 45 views
0

每次我跑我得到倒入回报/Reporting.aspx的全部内容,我完全难倒什么,我做错了你怎么做的WebMethod找到工作

我是一个完整的新手在JavaScript和Web开发,所以如果我没有提供所需的信息,问我会添加到这个问题

编辑:由于没有一个是有搞清楚什么是错我创建了一个运气新项目与相同的行为,我倾倒在这里很多

Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> 

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> 

     <script type="text/javascript"> 
      function loadReport() { 
       $.ajax({ 
        type: 'GET', 
        url: '/Default.aspx/search', 
        contentType: 'application/json; charset=utf-8', 
        dataType: 'json', 
        success: function (datareturn) { 
         alert(datareturn.d); 
        }, 
        failure: function (response) { 
         alert(response.d); 
        } 
       }); 
      } 
      function loadReport2() { 
       $.get('/Default.aspx/search') 
       .success(function (datareturn) { 
         alert(datareturn); 
        }) 
       .error(function (response) { 
         alert(response); 
        } 
       ); 
      } 
    </script> 
    <asp:Button Text="text" runat="server" OnClientClick="loadReport()" /> 
    <asp:Button Text="text" runat="server" OnClientClick="loadReport2()" /> 

</asp:Content> 

代码隐藏

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; 

namespace WebApplication1 
{ 
    public partial class _Default : Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { } 

     [WebMethod] 
     [ScriptMethod(UseHttpGet = true)] 
     public static string search() 
     { 
      return "worked"; 
     } 
    } 
} 

的Web.config

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=169433 
    --> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.6.1" /> 
    <httpRuntime targetFramework="4.6.1" /> 
    <pages> 
     <namespaces> 
     <add namespace="System.Web.Optimization" /> 
     </namespaces> 
     <controls> 
     <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" /> 
     </controls> 
    </pages> 
    <httpModules> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
     <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
    </httpModules> 
    </system.web> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <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> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules> 
     <remove name="ApplicationInsightsWebTracking" /> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
    </modules> 
    </system.webServer> 
</configuration> 

返回 output message

+0

'[的WebMethod]''预计POST'。 –

+0

我第一次尝试时,我无法得到那个工作,我试图简化 – MikeT

+0

@AlexKudryashev,不,你可以使用GET调用'webmethod' –

回答

0

这工作。下面是ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Reporting.aspx.cs" Inherits="WebApplication4.Reporting" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script src="Scripts/jquery-1.9.1.min.js"></script> 
    <script type="text/javascript"> 
     //NEXT line important, wait until DOM loads before assigning event 
      $(function() { 
      $(".load-report").on("click", function (e) { 
       e.preventDefault(); 
       var id = $('#<%= ReportsDropDown.ClientID %>').val(); 
       loadReport(id); 
      }); 
     }) 

     function loadReport(reportId) { 
      $.ajax({ 
       type: 'GET', 
       url: '/Reporting.aspx/search', 
       contentType: 'application/json; charset=utf-8', 
       dataType: 'json', 
       success: function (datareturn) { 
        alert(datareturn.d); 
       }, 
       failure: function (response) { 
        alert(response.d); 
       } 
      }); 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <div> 
      <asp:DropDownList runat="server" ID="ReportsDropDown" DataTextField="Text" DataValueField="Value" /> 
      <button class="load-report">Load</button> 
     </div> 
    </form> 
</body> 
</html> 

这里被后面的代码:

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

    [WebMethod] 
    [ScriptMethod(UseHttpGet = true)] 
    public static string search() 
    { 
     return "worked"; 
    } 
} 
+0

感谢您的时间,这绝对改变了一些,因为我可以跟踪JScri [t运行,但没有成功或失败触发 – MikeT

+0

深入挖掘一点后,这段代码是返回代码401未经授权 – MikeT