试图使用jQuery调用位于.aspx页面上的WebMethod

问题描述 投票:0回答:2

我正在尝试使用jQuery调用位于.aspx页面上的WebMethod。在开发环境(localhost)中工作正常,但是当我从服务器运行它时,似乎根本没有调用WebMethod。我正在使用的服务器环境工具是.Net 2.0 SP2,IIS7.5和Visual Studio 2005。

被调用的WebMethod将创建并写入一个.xml文件。然后,由于没有执行WebMethod并且从未创建过该文件,因此找不到该文件的另一种方法可以搜索该文件。

这些是我为解决问题而尝试过的一些事情:

•添加一个ScriptManager。我从.aspx文件中删除了该文件,因为它无法解决问题,并在博客中读到不需要ScriptManager即可使WebMethod调用起作用。

•在RadScriptManager中设置属性EnablePageMethods = true。目前,由于无法解决问题,因此不再设为true。

•在web.config文件中添加一个ScriptModule。

•在web.config中添加协议:

  <system.web>
     <webServices>
        <protocols>
           <add name="HttpSoap"/>
           <add name="HttpPost"/>
           <add name="HttpGet"/>
            <add name="Documentation"/>
        </protocols>
     </webServices>
   <system.web>

我从Web.config文件中删除了这些标记,因为它们不能解决问题,并在博客中读到在运行.net 1.1和IIS 5.0,IIS 5.1,IIS6.0的环境中需要这些标记。

•为用户和池分配写权限,以便他们能够写该文件夹。 (IIS 7.5)

我是否在Web.config文件中缺少某些内容?是什么引起了问题?

这里是WebMethod:

[WebMethod]
public static string GetContinuityofCareDocument(string continuityofCareDocument, string    indicator, string endOffile)
{
    string _filePrefix = indicator;
    string _fileNamexml = @"C:\TempFile" + _filePrefix + ".xml";
    StreamWriter _txtFile = new StreamWriter(_fileNamexml, true);
    _txtFile.Write(continuityofCareDocument);
    _txtFile.Close();

        return " Success";
}

这里是从中调用WebMethod的位置:

<script language="javascript" type="text/javascript">


function SendCcdtoServer(params,indicator,endOffile) {
    jQuery.ajaxSetup({ async: true });
    jQuery.ajax({
    type: "POST",
        url: '<%=ResolveUrl("WebServiceDisplayCCD.aspx")%>'+'/GetContinuityofCareDocument',
        data:  '{continuityofCareDocument: "'+params+'", indicator: "'+indicator+'", endOffile:             "'+endOffile+'"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        error: function(response) {
            alert("response + failure  ");
        }
    });
}
function OnSuccess(response) {
    alert("Success");
}


</script>

这里是Web.config:

<configuration>
    <configSections>
        <sectionGroup name="Localization">
            <section name="Localization"                        type="Localization.LocalizationConfigurationHandler, Localization,Version=1.0.0.0,  Culture=neutral, PublicKeyToken=CA5930580A5E0032" />
        </sectionGroup>
    </configSections>


    <system.web>

        <pages enableViewStateMac="false" validateRequest="true"                            enableEventValidation="false"   viewStateEncryptionMode="Never"                     smartNavigation="false" />
        <httpModules>
            <remove name="ScriptModule" />
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule,                        System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,                    PublicKeyToken=31bf3856ad364e35" />
        </httpModules>
        <httpHandlers>
            <add path="Telerik.Web.UI.WebResource.axd"                                  type="Telerik.Web.UI.WebResource" verb="*" validate="false" />
        </httpHandlers>
        <compilation debug="true">
            <assemblies>
                …
            </assemblies>
        </compilation>
        <sessionState mode="SQLServer" sqlConnectionString="Data                            Source=###.###.###.#;User ID=XXXXX;password=XXXXXX"                             cookieless="false" timeout="60" />
        <globalization enableClientBasedCulture="true" />

    </system.web>


    <location allowOverride="true" inheritInChildApplications="true">
        <appSettings>
            <add key="Authentication" value="Windows" />
        </appSettings>
    </location>

    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
            <add name="Telerik_Web_UI_WebResource_axd" verb="*"                                 preCondition="integratedMode" path="Telerik.Web.UI.WebResource.axd"                 type="Telerik.Web.UI.WebResource"/>
        </handlers>
    </system.webServer>

</configuration>

使用chrome的开发人员工具时,出现500错误,显示为“无效JSON”,并包含整个页面的脚本。

在寻找了可能的解决方案之后,我得到了很多关于如何解决“无效的JSON原语”错误的博客,该错误似乎不适用于我正在处理的问题。

可能是什么原因造成的?

c# jquery asp.net-ajax iis-7.5 webmethod
2个回答
0
投票

尝试设置一个解析变量并在调试中查看它:

var url = '<%=ResolveUrl("WebServiceDisplayCCD.aspx")%>'

0
投票

我有同样的问题。您找到解决方案吗?谢谢

© www.soinside.com 2019 - 2024. All rights reserved.