日期:2014-05-16  浏览次数:20330 次

Jquery用json调用wcf服务的问题
在一个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);
                }
            });

------解决方案--------------------
不要用http://pc1234567/Service/Service1.svc/hello这种地址,除非你的网站域名也是红色部分的,要不就跨域了

ajax请求地址是什么,自己核对下域名部分