【问题标题】:return text from asmx file using jquery ajax使用 jquery ajax 从 asmx 文件返回文本
【发布时间】:2013-09-02 07:55:40
【问题描述】:

我看不出这个简单代码的工作问题

setdata.asmx
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class setdata : System.Web.Services.WebService {

public setdata () {

    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
}

[WebMethod]
public string HelloWorld() {
    return "Hello World";
}

}

my jquery
$.ajax({
            method: "POST",
            url: "setdata.asmx/HelloWorld",
            contentType: "text/plain; charset=utf-8",
            //data: { category: category },
            success: function (txt) {
                alert(txt);
            },
            error: function(jqXHR, textStatus, errorThrown ){
          alert("Error:"+jqXHR + textStatus + errorThrown);
        }
        });

错误是:

[object Object]errorInternal Server Error

如何解决

我猜出了问题:-
jquery 位于我的网站名为 web1 的 admin 文件夹中的 default.aspx 页面内 我发布的网址只是 setdata.asmx , 服务器是否在根目录的当前目录中查找setdata.asmx

【问题讨论】:

  • 使用 F12 或 fiddler 查看来自服务器的响应
  • 如果你给你的webmethod设置断点,它会命中断点吗?
  • @nilesh thakkar -> 它没有到达断点
  • @devesh-> 我不知道那个方法,我必须在什么时候按 f12,为什么?
  • 如果您使用的是 IE 9 10 或 chrome,请点击 F12 打开开发者工具并帮助您在网络选项卡中查看响应msdn.microsoft.com/en-us/library/ie/gg589512(v=vs.85).aspx

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


【解决方案1】:

尝试更新 jQuery 脚本来检查错误:

 $.ajax({
        method: "POST",
        url: "<%= this.ResolveUrl("~/setdata.asmx")%>/HelloWorld",
        contentType: "text/plain; charset=utf-8",
        //data: { category: category },
        success: function (txt) {
            alert(txt);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("Error:" + jqXHR + textStatus + errorThrown);
        }
    });

这应该会简化调试。 Aldo 使用ResolveUrl 方法来获取服务的正确地址。

【讨论】:

    【解决方案2】:

    请确保您已添加必要的 web.config 条目以从客户端脚本调用 asmx 服务。检查以下网址:

    http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/

    【讨论】:

    • 我使用的是 asp.net 第 4 版,链接中没有相应的程序集
    • 3.5+ – 从 .NET 3.5 开始,System.Web.Extensions 随框架一起提供。无需安装其他组件。只要确保你有那些 web.config 条目并且你到达了断点。要进行错误检查,请按 F12 访问 Web 浏览器控制台。对于 FF,请确保您已安装 Firebug 插件。
    • 您的 web.config 文件中是否有我提供的链接中的博客文章中提到的那些条目?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2015-08-20
    • 2022-01-10
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    相关资源
    最近更新 更多