javascript JQuery ajax调用跨域webservice

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6117924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:33:23  来源:igfitidea点击:

JQuery ajax call to cross-domain webservice

javascriptjqueryasp.netweb-services

提问by Andrew Kalashnikov

I would like consume cross-domain web-service from client with jquery

我想使用 jquery 从客户端使用跨域 Web 服务

function TestService() {
    $.ajax({
        url: "http://service.asmx/GetGeoCompletionList",
        data: { "prefixText":"eka", "count":"10", "contextKey":"Trace_0$Rus" },
        dataType: "jsonp",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            alert(data);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.statusText);
        }
    });
}

At the error hander I have: textStatus=parseerror

在错误处理程序中,我有: textStatus=parseerror

XMLHttpRequest has status 200and readyState 4

XMLHttpRequest 有 status200和 readyState4

errorThrown is jQuery16103495572647140...78197139 was not called

错误抛出是 jQuery16103495572647140...78197139 was not called

I've spent on it for a lot of hours and couldn't make it work. Can u help me?

我已经在它上面花费了很多时间,但无法让它发挥作用。你能帮我吗?

UPDATED

更新

Thanks, I change to GET, and fix my data string.

谢谢,我更改为 GET,并修复我的数据字符串。

Service returns valid JSON object. I can see it at firebug on other site, which consume this service. But that site is getting common json(since it has one domain).

服务返回有效的 JSON 对象。我可以在使用此服务的其他站点的 firebug 上看到它。但是该站点正在使用通用 json(因为它只有一个域)。

So, if the web-service returns valid JSON(not jsonp), I can't use the same method with jsonp? What can I do for consiming json web-service from other domain?

那么,如果网络服务返回有效的 JSON(不是 jsonp),我不能对 jsonp 使用相同的方法?我可以做什么来使用来自其他域的 json 网络服务?

回答by Quentin

That string you are passing and claiming is JSON isn't JSON.

您传递并声称是 JSON 的字符串不是 JSON。

Only "characters may be used to quote strings.

只能使用"字符来引用字符串。

Also, you can't make a cross domain JSON-P POST request.

此外,您不能发出跨域 JSON-P POST 请求。

回答by lonesomeday

You cannot do cross-domain POST requests using JSONP. JSONP works by adding scripttags to the page. scripttags always fetch their source using GET HTTP requests. You won't get any data posted.

您不能使用 JSONP 执行跨域 POST 请求。JSONP 通过向script页面添加标签来工作。 script标签总是使用 GET HTTP 请求来获取它们的来源。你不会得到任何发布的数据。

回答by jvenema

Summary of problems:

问题总结:

  1. Make sure you're using valid JSON as @Quentin mentioned.
  2. Make sure you're using GET requests, as @lonesomeday mentioned
  3. Make sure the response from the server is JSONP, as seen here
  1. 确保您使用的是@Quentin 提到的有效 JSON。
  2. 确保您使用的是 GET 请求,如@lonesomeday 所述
  3. 确保来自服务器的响应是 JSONP,如下所示