无法使用 jquery json 调用 webmethod

发布于 2024-09-18 10:37:14 字数 808 浏览 0 评论 0原文

我使用 jsonp 协议来调用 Web 方法。

我将这段代码用于网络服务:

public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}

这在 Jquery 上,客户端有 jason:

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: 'http://localhost:50837/Service1.asmx/HelloWorld',
            data: {},
            dataType: "json",

            success: function(Msg) {
                alert('success:' + Msg.d.FirstName);

            },
            error: function(xhr, textStatus, errorThrown) {
                alert("error");
            }

        });

    }

这个 Jquery 总是给我一条错误消息,但我不知道原因。 有人可以帮助我吗?

I use the protocol jsonp to call web methods.

I use this code for the webservice:

public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}

And this on Jquery with jason on client side:

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: 'http://localhost:50837/Service1.asmx/HelloWorld',
            data: {},
            dataType: "json",

            success: function(Msg) {
                alert('success:' + Msg.d.FirstName);

            },
            error: function(xhr, textStatus, errorThrown) {
                alert("error");
            }

        });

    }

This Jquery gives me always an error message, but I don't know the reason.
Someone can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙味迷妹 2024-09-25 10:37:14

应该将Web 服务作为网站的一部分放置。如果您不这样做,您的代码将无法工作,因为同源策略问题(请参阅)。

您可以在同一个站点上放置许多使用不同技术开发的应用程序,例如 ASP.NET MVC、ASMX Web 服务和 WCF 服务,并且所有应用程序都可以很好地协同工作。这是无需更复杂的 JSONP 即可实现的唯一方法。在你的情况下,JSONP 太大了。

您的问题几乎与 使用 JSON 从 AJAX 和 JQuery 调用简单的 Web 服务(.asmx 文件) - 解析错误。如果您需要一个有效的 Hello Wold 示例,您将找到完整代码示例的 url。

您可以在有关发出 XHR 请求的问题。读完之后你就会明白同源策略问题确实很复杂。您可以解决它,但在您的情况下,您实际上并没有需要将所有内容放在同一个网站上并使用相对路径。

You should place Web Service as a part on the site. If you don't do this your code will not work because of Same Origin Policy problem (see ).

You can place on the same site many application developed with different technique like ASP.NET MVC, ASMX Web Service and WCF service and all can work together very well. It's the only way to be able without more complex JSONP. In you situation JSONP is oversized.

You question is almost the same as Calling simple web service (.asmx file) from AJAX and JQuery using JSON - parse error. If you need a working Hello Wold example you will find an url to the full code example.

You can read more about different ways of solving Same Origin Policy under Question about making XHR requests. After reading of that you will understand that Same Origin Policy problem is really complex. You can solve it, but in your case you not really have it need just place all on the same web site and use relative paths.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文