【问题标题】:Cannot access Webservice client side using Jquery JSON无法使用 Jquery JSON 访问 Webservice 客户端
【发布时间】:2011-10-10 17:03:35
【问题描述】:

我无法通过 jquery 访问 Web 服务,也找不到问题所在,我已经检查了所有内容,但似乎没有任何效果。

这是我的 javascript 代码:

function obtenerMunicipios() {
    $.ajax({
        type: "POST",
        url: "WebService.asmx/ObtenerMunicipios",
        data: { sEstado: "info" },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) { alert(response.d); },
        error: function (response) { alert("Error"); }
    });
}

当 DropDownList 发生变化时调用,我已经在 web 服务中测试了 WebMethod 并且它工作正常,它返回一个 ArrayList 顺便说一下,我已经分别放置了 [ScriptService] 和 [WebMethod]。它总是告诉我有一个错误。

Visual Studio 将代码文件放在 App_Code 中,但 asmx 文件放在 root 我不知道这是否有问题,但我不这么认为,因为我可以访问 webmethod 服务器端。

结果总是向我显示错误警报,如果我在 WebMethod 中放置断点,它永远不会到达那里,所以我认为这是 URL 的问题。

我能做什么?

提前致谢

【问题讨论】:

    标签: jquery asp.net ajax web-services web


    【解决方案1】:

    尝试检查返回错误 将您的“错误:”更改为此

    error:function (xhr, ajaxOptions, thrownError){
                        alert(xhr.responseText);
                        alert(xhr.status);
                        alert(thrownError);
                    }
    

    您可以在 Firebug、Fiddler 或 IE9 开发工具中看到更多内容

    你也可以在你的页面上创建 webmetod,服务器端是这样的:

    public partial class _Default : Page 
    {
      [WebMethod]
      public static string GetDate()
      {
        return DateTime.Now.ToString();
      }
    }
    

    在客户端改变这个

    url: "PageName.aspx/GetDate"
    

    以 Dave 页面为例: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

    【讨论】:

      【解决方案2】:

      如果您还没有,请查看通过 fiddler 发回的内容。我的猜测是它的发送 回到它喜欢的东西。

      您需要使用beforeSend event 指定您的请求将接受什么。

      function obtenerMunicipios() 
      { 
           $.ajax({ 
                    type: "POST", 
                    url: "WebService.asmx/ObtenerMunicipios", 
                    data: { sEstado: "info" }, 
                    contentType: "application/json; charset=utf-8", dataType: "json",
                    success: function (response) { alert(response.d); }, 
                    error: function (response) { alert("Error"); },
                    //This is the line you're looking for I think.
                    beforeSend: function(xhr, settings) { xhr.setRequestHeader("Accept",
                                                                    "application/json"); } 
               }); 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-23
        • 1970-01-01
        • 1970-01-01
        • 2014-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多