Jquery用json调用wcf服务的问题

lyh_mzsm 2013-10-23 04:59:11
在一个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);
}
});
...全文
121 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyh_mzsm 2013-10-24
  • 打赏
  • 举报
回复
OK了,重新作了个WCF服务器。 现在应用URL直接访问是建立一个服务器工厂。

<%@ServiceHost Language="c#" Debug="true" Service="xx.xx" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
这样就能直接访问了,但是web.config里面的配置需要删掉。删掉之后,在asp.net的客户端却不能引用参照了,比较头疼。这个先结掉了
lyh_mzsm 2013-10-23
  • 打赏
  • 举报
回复
xhr.status :400 xhr.statusText: Bad Request 果然是URL不对阿
lyh_mzsm 2013-10-23
  • 打赏
  • 举报
回复
引用 3 楼 showbo 的回复:
不要用http://pc1234567/Service/Service1.svc/hello这种地址,除非你的网站域名也是红色部分的,要不就跨域了 ajax请求地址是什么,自己核对下域名部分
是同一个域。 刚才重新建了个本地的wcf服务,直接用本地的也不行。 http://localhost:1336/Service1/Service1.svc/hello 错误信息:xhr.responseText都是空,什么都没有。
  • 打赏
  • 举报
回复
不要用http://pc1234567/Service/Service1.svc/hello这种地址,除非你的网站域名也是红色部分的,要不就跨域了 ajax请求地址是什么,自己核对下域名部分
lyh_mzsm 2013-10-23
  • 打赏
  • 举报
回复
引用 1 楼 showbo 的回复:
感觉是跨域了,用的什么浏览器?非IE端口不一样也算跨域的
用的IE8。
  • 打赏
  • 举报
回复
感觉是跨域了,用的什么浏览器?非IE端口不一样也算跨域的

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧