【发布时间】:2016-04-17 09:19:32
【问题描述】:
我有一个方法 WCF,它返回一个 JSON: enter image description here
客户端有一个脚本应该从 wcf 服务获取数据 脚本:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
$(document).ready(function () {
$('#btn').click(function () {
$.ajax({
url: 'http://192.168.200.100/Searching.BE.Service//WCFRESTService.svc/GetCategories',
method: 'get',
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success:function(data)
{
alet(data.Announcing[0].Categories.id);
},
error: function (error)
{
alert(error);
}
})
var request = createCORSRequest("get", "http://192.168.200.100/Searching.BE.Service//WCFRESTService.svc/GetCategories");
request.send();
})
})
</script>
<input id="btn" type="button" />
点击按钮后我有这个错误:对象对象
我有控制台消息:
SCRIPT7002:XMLHttpRequest:网络错误 0x80070005,拒绝访问。
SEC7120: Source http://localhost:4945 is not found in header Access-Control-Allow-Origin ..
如何解决这些问题?
【问题讨论】:
-
你能通过浏览器浏览这个吗
http://192.168.200.100/Searching.BE.Service//WCFRESTService.svc -
@MairajAhmad,这是因为浏览器i.stack.imgur.com/QlOis.png
标签: javascript jquery asp.net ajax asp.net-mvc