在一个html里面调用其他机器的wcf服务,请问url是个什么规则啊。
我现在已经搭建好一台wcf服务,可以通过http://pc1234567/Service/Service1.svc来访问;并且在本机上面新建一个asp.net的web项目,在新建的aspx页面中成功访问了服务器并取得返回值。
现在是因为手机也需要访问,暂时作了个一个html页面,在jquery里面通过json访问wcf服务器,一直失败。但是如果我把这个页面加在到本地wcf服务器的工程里面,通过:Service1.svc/hello可以访问到,但是如果直接访问wcf服务器: http://localhost:2999/Service/Service1.svc/hello就不行了。            ar obj = { item: {item1:1,item2:2}
                     };
            var arg = JSON.stringify(obj);
            $.ajax({
                type: "Post",
                datatype: "json",
                url: "http://pc1234567/Service/Service1.svc/hello",
                data: arg,
                contentType: "application/json; charset=utf-8",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Accept", "application/json")
                },
                success: function (data, textStatus, xhr) {
                    if (data != null) {                        $("#txtmsg").val(data.msg);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert(xhr.responseText);
                }
            });

解决方案 »

  1.   

    不要用http://pc1234567/Service/Service1.svc/hello这种地址,除非你的网站域名也是红色部分的,要不就跨域了ajax请求地址是什么,自己核对下域名部分
      

  2.   

    是同一个域。刚才重新建了个本地的wcf服务,直接用本地的也不行。
    http://localhost:1336/Service1/Service1.svc/hello
    错误信息:xhr.responseText都是空,什么都没有。
      

  3.   

    xhr.status :400
    xhr.statusText: Bad Request
    果然是URL不对阿
      

  4.   

    OK了,重新作了个WCF服务器。
    现在应用URL直接访问是建立一个服务器工厂。<%@ServiceHost Language="c#" Debug="true" Service="xx.xx" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>这样就能直接访问了,但是web.config里面的配置需要删掉。删掉之后,在asp.net的客户端却不能引用参照了,比较头疼。这个先结掉了