【问题标题】:jQuery $.ajax not calling ASP.NET web service method in IEjQuery $.ajax 不在 IE 中调用 ASP.NET Web 服务方法
【发布时间】:2011-09-07 23:07:38
【问题描述】:

我一直在尝试通过 IE 使用 $.ajax 方法从 jQuery 调用 Web 服务,但它似乎不起作用。我查看了 Stackoverflow 和大量的 Google 搜索,但人们提出的解决方案都没有为我工作。

以下是事情的设置...

jQuery 调用:

function PerformPainPointDataSubmit() {

    var coordString = "";

    var mapAreas = $('map[id$=body_profile_map]').find('area');

    mapAreas.each(function() {
        if ($(this).attr('shape').toLowerCase() == "circle") {
            coordString += $(this).attr('coords') + ';';
        }
    });

    $.ajax({
        type: "POST",
        url: "../../LandingPageWebService.asmx/PerformPainPointDataSubmit",
        data: JSON.stringify({ "coords": coordString }), 
        //data: "{'coords':'" + coordString + "'}", // Doesn't work
        //data: '{ "coords": "' + coordString + '" }'  // Also doesn't work
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        async: true,
        cache: false,
        success: function(data) {
            alert("success: " + status);
        },
        failure: function(msg) {
            alert("fail: " + msg);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            debugger;
        }
    });
    //$.delay(1000); <-- Explained a little further down
    //return false;
}

请注意,successfailureerror 函数永远不会被调用。

以下是LandingPageWebService类的定义(部分数据库代码已被删除):

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class LandingPageWebService  : System.Web.Services.WebService {

    [WebMethod(EnableSession=true)]
    public bool PerformPainPointDataSubmit(string coords)
    {
    string hotSpotCoords = string.Empty;

    string[] coordsSplit = coords.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

    // parse the string and make sure we only have 
    // values and nothing else. 
    foreach (string spl in coordsSplit)
    {
        string[] indCoords = spl.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

        if (indCoords.Length != 3)
        {
            return false;
        }
        int x = 0;
        int y = 0;

        try
        {
            x = int.Parse(indCoords[0]);
            y = int.Parse(indCoords[1]);

        }
        catch (FormatException formEx)
        {
            return false;
        }

        hotSpotCoords += x + "," + y + ";";
    }
    // snipped out Database saving code                
    return true; 

}

jQuery 函数在按钮内的 OnClientClick 中调用:

<asp:Button ID="btnSave" Text="Save" runat="server" OnClientClick="PerformPainPointDataSubmit();CloseWin()" CssClass="STD_Button" ValidationGroup="vgEditClinicalSummary" CausesValidation="true" />

页面本身位于一个模式对话框中,单击“保存”按钮时该对话框关闭。无论我调用多少次,在 Chrome 和 Firefox 中调用 Web 服务都没有任何问题。然而,使用 IE 就变成了废话。

通常,但并非一直如此,它会在页面第一次加载时被调用。我认为存在缓存问题,但 cache:false 已经设置好了。我尝试在网址中添加DateTime,但我不断收到错误(老实说,我认为我的格式不正确,建议?)。我尝试了不同的datatype 字符串,当然JSON.stringify() 可以工作,但就像我说的,它只能工作一次。

我注意到,当我在 jQuery 函数中中断时,如果我等待一两秒,IE 实际上会调用 Web 服务并成功执行。每次都会这样做。我认为模式窗口的关闭速度比服务器处理请求的速度更快,而不是进行 Web 服务调用。我在 jQuery 代码中添加了$.delay(1000),希望它能起作用,但不幸的是它没有。

现在我束手无策,完全不知道如何进行。一切似乎都合乎逻辑,但显然有些不对劲。

如果任何人都可以提供帮助,我将不胜感激。

【问题讨论】:

  • 首先,不要使用 $.delay() 函数,而是尝试使用 setTimeout() 原生 JS 函数。除了以 JSON 格式发送坐标并解决确保 JSON.stringify() 与您的 IE 版本兼容的问题之外,为什么不以不同的格式发送数据呢?您可以尝试使用 jQuery 的 $.param() 函数将数组或对象转换为序列化字符串以发送到 Web 服务脚本。解析后者比解析 JSON 对象要容易得多。
  • 仅供参考 'failure' 不是有效的 $.ajax 选项。还有,为什么不在 ajax 'complete' 中关闭你的对话框(或者如果你想使用'error' 来报告错误,则为'success')?
  • @Link 我从代码中删除了$.delay(),因为下面 JNappi 的建议起到了作用。此外,我认为以字符串形式发送坐标是最简单的,而且说实话,我对 Web 技术还很陌生,对 jQuery、JSON 和其他事物的一些细微差别对我来说仍然很陌生。感谢您的建议。 @Lain,我删除了失败(不确定我在示例中看到的位置),如果成功,我将尝试关闭窗口。感谢您的帮助和建议。

标签: jquery asp.net ajax web-services


【解决方案1】:

这听起来与此处描述的问题相似: Ajax request with JQuery on page unload

调用需要同步以保证在窗口关闭之前完成。

【讨论】:

  • 这似乎可以解决问题。我以为我已经对我的问题进行了非常彻底的搜索,但显然没有(也许我的搜索要简洁)。我确实遇到过将async 更改为 false 的建议。当我尝试它时,浏览器陷入了困境。很有可能我在某一点上进行了多项更改,从而导致了一整套问题。谢谢您的帮助!非常感谢。
  • 没问题。我碰巧知道去哪里找,因为我最近遇到了一个类似的问题。
猜你喜欢
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 2011-05-26
相关资源
最近更新 更多