简体   繁体   English

WCF javascript 跨域调用

[英]WCF javascript call cross domain

I am new to javascript and I have a problem that is giving me hard times.我是 javascript 的新手,我遇到了一个让我很难过的问题。

I want to put in a page a tracking javascript.我想在一个页面中添加一个跟踪 javascript。 This script will call a wcf service to track client browser information, page visited and timestamp.该脚本将调用 wcf 服务来跟踪客户端浏览器信息、访问的页面和时间戳。 The problem is that I get: "405 Method Not Allowed" error on javascript call.问题是我在 javascript 调用上得到:“405 Method Not Allowed”错误。 The service will be on another domain.该服务将在另一个域上。 Now I test this on localhost.现在我在 localhost 上测试它。 The service is working fine because I can call it from a new page in browser.该服务运行良好,因为我可以从浏览器的新页面调用它。 Did you experienced the same problem?你遇到过同样的问题吗? How can I fix this?我怎样才能解决这个问题?

My javascript code is:我的 javascript 代码是:

Wcf service code: Wcf 服务代码:

[ServiceContract(Name = "CustomersAssistantService", Namespace = "CustomersAssistantService")]
public interface ICustomersAssistantService
{
    [OperationContract]
    [WebGet]
    string DoWork();

    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    string Sum(int n1, int n2);
}

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CustomersAssistantService : ICustomersAssistantService
{
    public string DoWork()
    {
        return "work done";
    }

    public string Sum(int n1, int n2)
    {
        return (n1 + n2).ToString();
    }
}

Javascript call: Javascript 调用:

function CallWcf1(){
        var _I = this;
        var url = "http://localhost:58399/CustomersAssistantService.svc/customersAssistantService/";        

            var methodUrl = _I.serviceUrl + 'dowork';

             $.ajax( {
                        url: methodUrl,
                        data: '',
                        type: "GET",
                        processData: false,
                        contentType: "application/json",
                        timeout: 10000,
                        dataType: "text",  // not "json" we'll parse
                        success: 
                        function(res) 
                        {                                    
                            if (!callback) return;
                            // *** Use json library so we can fix up MS AJAX dates
                            var result = JSON2.parse(res);
                            // *** Bare message IS result
                            if (bare)
                            { callback(result); return; }
                            // *** Wrapped message contains top level object node
                            // *** strip it off
                            for(var property in result)
                            {
                                callback( result[property] );
                                break;
                            }                    
                        },
                        error:  
                        function(xhr)
                        {
                            if (!error) return;
                            if (xhr.responseText)
                            {
                                var err = JSON2.parse(xhr.responseText);
                                if (err)
                                    error(err); 
                                else    
                                    error( { Message: "Unknown server error." })
                            }
                            return;
                        }
                    });   
    }

Am I doing something wrong?难道我做错了什么?

Thanks a lot, Radu D非常感谢, 拉杜 D

You're running into the Same Origin Policy .您遇到了 Same Origin Policy Ajax requests are limited to talking to the same origin ; Ajax 请求仅限于与同源通信; cross-domain requests will typically be denied.跨域请求通常会被拒绝。

Depending on your needs, there are various approaches:根据您的需要,有多种方法:

  • If your users will be using only quite modern browsers (so, not IE7), you could implement Cross Origin Resource Sharing on your server.如果您的用户将只使用非常现代的浏览器(因此,不是 IE7),您可以在您的服务器上实现跨域资源共享 This is a standard from the W3C that allows a server to open up access to its resources cross-origin, but it's only supported in modern browsers.这是 W3C 的标准,允许服务器开放对其资源的跨域访问,但仅在现代浏览器中受支持。 (And on IE, it's supported, but not via the usual XMLHttpRequest object; instead, you have to use the XDomainRequest object.) (在 IE 上,它受支持,但不是通过通常的XMLHttpRequest object;相反,您必须使用XDomainRequest object。)
  • You could implement a JSON-P interface .您可以实现JSON-P 接口
  • If all you want to do is send a notification to the other domain, you can just load a resource from it (an img , a script , whatever) by adding those elements with the relevant src attribute;如果您只想向另一个域发送通知,您可以通过添加具有相关src属性的元素从中加载资源(一个img 、一个script等); this means, though, that you're limited to using the GET method which is supposed to be for idempotent resources.但是,这意味着您仅限于使用应该用于幂等资源的GET方法。

As far as I know, request for WCF must use the POST method, so change to:据我所知,对 WCF 的请求必须使用 POST 方法,所以改为:

type: "POST", 

In the AJAX call.在 AJAX 调用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM