【问题标题】:Cross domain webmethod giving 204 no content跨域webmethod给出204没有内容
【发布时间】:2014-01-28 07:52:06
【问题描述】:

我在 ASP.NET 中创建了一个 Web 方法,并尝试通过 jQuery AJAX 访问它。当我通过浏览器调用 Webmethod 时,它会给出 JSON 结果,但是当我通过 jQuery AJAX 调用它时,它会在 Firefox 的 Firebug 中给出“204 no content”。 (我的 webmethod 在一个域中,而 jQuery AJAX 代码在另一个域中)。

JQuery Ajax 代码:

 $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "http://localhost:1177/api/authentication/getusermenu?roleid=1",
           processData: true,
            dataType: "jsonp",
            success: function (msg) {
                alert(msg.d);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var data = $.parseJSON(jqXHR.responseText);
                alert(data);
            }
        });

Firebug 输出:

【问题讨论】:

  • http://*ocalhost:1177/api/authentication/getusermenu?roleid=1 在浏览器中点击它,看看你会得到什么
  • 嗨 Jai,我正在使用该 URL 获得 JSON 响应
  • 您的服务器端代码是否正确处理 JSONP 请求?

标签: jquery ajax json cross-domain


【解决方案1】:

试试下面的: 1. 如果 webmethod 在同一个应用程序中,则不需要提供 localhost 或任何东西。如果 web 方法在任何其他机器上并且可以访问,则提供 url: "http:///..." 而不是 localhost:portNo。 2.数据类型:“json”,

完整示例如下:

 $.ajax({
            type: "POST",
            url: "ABC.aspx/GetUserMenu?roleid=1",
            data: {},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
             success: function (msg) {
            alert(msg.d);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            var data = $.parseJSON(jqXHR.responseText);
            alert(data);
        }

        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多