【问题标题】:Calling Web Service from jQuery .ajax() issue从 jQuery .ajax() 问题调用 Web 服务
【发布时间】:2013-08-03 20:29:01
【问题描述】:

我在调用跨域 Web 服务时遇到问题。我在这里阅读了一些关于它的文章,但我并没有真正找到解决方案。我刚刚明白我需要json 格式的数据,因为我在尝试从服务中获取xml 数据时总是得到Error: Access denied.,但现在我遇到了不同的问题。这是我的.ajax() 电话:

$.ajax({
            type: "GET",
            contentType: "application/jsonp; charset=utf-8",
            url: "http://tomas/_vti_bin/EmmaService.asmx/GetResult",
            dataType: "jsonp",
            data: {
                value : "testValue",
                converstionId : "testId"
            },
            success: function(resp) {
                alert("success: " + resp);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("error status: " + xhr.status);
                alert("error status text: " + xhr.statusText);
                alert("error response text: " + xhr.responseText);
            },
        });

由此我收到以下 3 个警报错误:

error status: 200
error status text: success
error response text: undefined

我不明白的是error status text: success。

我的网络服务中的代码:

[WebMethod(EnableSession = false, Description = "Gets result")]
    public EmmaServiceResult GetResult(string value, string converstionId)
    {
        ...
        return result;
    }

关于如何使它工作的任何建议?谢谢! :)

【问题讨论】:

  • 注意:我无法更改 Web 服务,它正在被其他解决方案使用,我需要正确调用它并在没有任何代码的情况下工作,只需 javascript,因为我正在创建一个 SharePoint 2013 应用程序,它不允许隐藏代码:/
  • 您能否在 Firefox 和/或 Chrome Web Inspector 上的 Firebug 中运行此程序,然后将整个请求(文本)和响应(同上)剪切并粘贴到问题中?
  • 检查浏览器开发工具中的 javascript 控制台和网络选项卡。查找任何错误,并查看 xhr 响应,看看它是否符合您的预期。
  • 我收到Uncaught TypeError: Object [object Object] has no method 'autocomplete'

标签: javascript jquery ajax json web-services


【解决方案1】:

尝试将?callback=? 添加到您的网址末尾:

http://tomas/_vti_bin/EmmaService.asmx/GetResult?callback=?

另外,请尝试查看 throwedError 以确定错误是什么:

alert("error response text: " + thrownError);

可能是解析错误等。实际上与 ajax 请求无关,而是您定义应如何处理响应。

另外,查看here 了解如何从 WCF 服务返回 json。

[WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "players")]

【讨论】:

  • 我已将其更改为xhr.thrownError,它仍然返回undefined:/
  • 现在我得到error response text: jQuery...lots of numbers... was not called
  • 是的,尝试从 jsonp 更改为 json,这可能会让我们更接近一点
  • 现在我得到error status: 0 and the response text: Access denied.
  • 您调用的网络服务在不同的域中?
【解决方案2】:

我最近在通过 AJAX 调用发出跨域请求时遇到了很多问题。我们最终在无需修改 API 的情况下让它工作,但我们确实需要访问托管 API 的服务器,因此我们可以让它在响应中发送一些标头。但是整个问题调试起来很痛苦,我发现所有浏览器都很难报告有意义的错误。因此,如果这不能解决您的问题,这可能对您不起作用并提前道歉。

该解决方案要求您发出 CORS 请求,并向服务器响应添加一些标头。这些页面都是很好的资源:

https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS http://www.html5rocks.com/en/tutorials/cors/

我认为在您的情况下,由于您正在发出基本请求并且您没有处理 cookie,您可以让您的 .ajax 调用基本保持不变,只需将 dataType 更改为“json”,将 contentType 更改为“application/json " 如果您要发送 JSON。

然后,您必须修改服务器,使其通过将这些标头添加到响应中来处理 CORS 预检请求:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: Content-Type

(见这个问题:jQuery CORS Content-type OPTIONS)

希望这对你有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多