【问题标题】:how to consuming web service using jquery ajax in asp.net web application如何在 asp.net Web 应用程序中使用 jquery ajax 使用 Web 服务
【发布时间】:2016-11-08 10:59:59
【问题描述】:

我无法使用 jquery ajax 使用 Web 服务,但可以使用 C# 语言。 这是我的 jquery Ajax 代码`

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-3.0.0.js"></script>
    <script>
        $(document).ready(function(){


            var url = "http://192.xxxx.xx.xx:10000/service1.asmx/GetJsonData";

            $.ajax({
                type: "GET",

                url: url,

                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert("hii");
                    alert(data.d)
                },
                error: function (xmlHttpRequest, textStatus, errorThrown) {
                    alert("error");
                }
            });


        })
    </script>
</head>
<body>
    <form id="form1"  runat="server">
    <div>

    </div>
    </form>
</body>
</html>

The above code is not working but when i am trying the same using  C#  language it is working fine.

错误是:网络错误:IE浏览器拒绝访问。

Google Chrome 中的错误如下所示。 XMLHttpRequest 无法加载 http://192.xxxx.xx.xx:10000/service1.asmx/GetJsonData.No 请求的资源上存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:49951' 因此不允许访问。

`

【问题讨论】:

    标签: c# jquery ajax web-services


    【解决方案1】:

    显然这是一个 Ajax 问题,与 C# 无关。看这里:"No 'Access-Control-Allow-Origin' header is present on the requested resource"

    【讨论】:

    • 嗨@thcerutti,我不明白,你能建议我在我的代码中哪里做错了吗?如何解决这个错误。
    【解决方案2】:

    可能的问题

    似乎是CORS 相关问题。

    来自链接:

    出于安全原因,浏览器会限制跨域 HTTP 请求 从脚本中启动。

    当您尝试从另一个域访问一个域的资源时会发生这种情况。示例:twitter.com 中的脚本尝试访问 facebook.com API 端点。


    可能的解决方案

    如果是这样的话:

    • 如果您可以控制服务器,则可以根据需要设置Access-Control-Allow-Origin
    • 如果您不控制服务器,您可以查看为请求提供服务的 Web 应用程序是否有任何方法来允许来自其他域的请求
      • 根据提供的 OAuth 令牌设置适当的 Access-Control-Allow-Origin
      • 将来自目标 Web 应用程序的页面加载到 IFrame 中,这将代表您发出请求。使用 browser's messaging API 与 IFrame 通信。

    【讨论】:

    • 嗨@Fa773N,我无法访问服务器,请告诉我在我的应用程序中完成设置的过程。就像在 eb.config 或我的 aspx.cs 代码中一样。跨度>
    • @GOPALYADAV:我不熟悉 asmx 技术。不过,快速搜索让我找到了this question。试试看是否可以使用此方法设置标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多