简体   繁体   English

尝试使用 jQuery 调用驻留在 .aspx 页面上的 WebMethod

[英]Trying to call a WebMethod that resides on an .aspx page using jQuery

I'm trying to call a WebMethod that resides on an .aspx page using jQuery.我正在尝试使用 jQuery 调用驻留在 .aspx 页面上的 WebMethod。 In the development environment (localhost) works fine, but when I run it from the server it seems like the WebMethod is not called at all.在开发环境 (localhost) 中工作正常,但是当我从服务器运行它时,似乎根本没有调用 WebMethod。 The server environment tools I'm using are .Net 2.0 SP2, IIS7.5, and Visual Studio 2005.我使用的服务器环境工具是 .Net 2.0 SP2、IIS7.5 和 Visual Studio 2005。

The WebMethod that is being called creates and writes an .xml file.被调用的 WebMethod 创建并写入一个 .xml 文件。 This file is then searched by another method that doesn't find it since the WebMethod is not executed and the file was never created.由于未执行 WebMethod 并且从未创建该文件,因此该文件随后由找不到它的另一种方法搜索。

These are some of the things I've tried in order to fix the problem:这些是我为解决问题而尝试的一些方法:

• Add a ScriptManager. • 添加脚本管理器。 I deleted it from the .aspx file since it didn't fix the problem and read in a blog that a ScriptManager wasn't needed in order to make the WebMethod call works.我从 .aspx 文件中删除了它,因为它没有解决问题,并在博客中读到不需要 ScriptManager 来使 WebMethod 调用工作。

• Set the property EnablePageMethods = true in the RadScriptManager. • 在RadScriptManager 中设置属性EnablePageMethods = true。 Currently it's not set to true anymore since it didn't fix the problem.目前它不再设置为 true,因为它没有解决问题。

• Add a ScriptModule in the web.config file. • 在web.config 文件中添加ScriptModule。

• Add the the Protocols in the web.config: • 在 web.config 中添加协议:

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

I deleted these from the Web.config file since they didn't fix the problem and read in a blog that these tags were needed in environments running with .net 1.1 and IIS 5.0, IIS 5.1, IIS6.0.我从 Web.config 文件中删除了这些,因为它们没有解决问题,并在博客中读到在运行 .net 1.1 和 IIS 5.0、IIS 5.1、IIS6.0 的环境中需要这些标签。

• Assign write permissions to users and pools in order for them to be able to write to the folder. • 为用户和池分配写入权限,以便他们能够写入文件夹。 (IIS 7.5) (IIS 7.5)

Am I missing something in the Web.config file?我在 Web.config 文件中遗漏了什么吗? What could be causing the problem?什么可能导致问题?

Here is the WebMethod:这是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";
}

Here is the where the WebMethod is being called from:这是调用 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>

Here is the Web.config:这是 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>

When using chrome's developer tools I get a 500 error, which states "Invalid JSON" and contains the script of the whole page.使用 chrome 的开发人员工具时,我收到 500 错误,其中指出“无效 JSON”并包含整个页面的脚本。

After searching for possible solutions to this error I get many blogs about how to solve an "Invalid JSON primitive" error, which doesn't seem to apply to the problem I'm dealing with.在搜索此错误的可能解决方案后,我收到了许多关于如何解决“无效的 JSON 原语”错误的博客,这似乎不适用于我正在处理的问题。

What could be causing this?什么可能导致这种情况?

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

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

I have spent a lot of time looking for the answer to this but could not find it on any site, even though it is a very simple answer: It depends on parameter location in development mode and publish mode on internet.我花了很多时间寻找这个问题的答案,但在任何网站上都找不到,尽管这是一个非常简单的答案:它取决于开发模式下的参数位置和互联网上的发布模式。

In develop mode your URL is like this http:// localhost:portnumber/yournamesite/pagesite/parameter and location of your parameter sit on position 4 of url.在开发模式下,您的 URL 是这样的http:// localhost:portnumber/yournamesite/pagesite/parameter并且您的参数位置位于 url 的位置 4。

When you publish your site on internet on iis, your URL becomes http://yoursite.com/pagesite/parameter and location of your parameter is on position 3.当您在 iis 上在 Internet 上发布您的网站时,您的 URL 变为http://yoursite.com/pagesite/parameter并且您的参数位置位于位置 3。

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

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