我该如何解决这个错误

时间:2016-05-13 05:58:10

标签: javascript jquery ajax html5 cordova

我正在尝试使用AJAX调用 WCF服务,下面是我的代码:

$.ajax({
    url: "http://localhost/TestingServices/Service1.svc/GetData"
    data: "{'value:1}",
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        alert(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
    }
});

但执行后它给了我以下错误:

XMLHttpRequest cannot load 
  http://localhost/TestingServices/Service1.svc/GetData.Response to 
  preflight request doesn't pass access control check: 
  No 'Access-Control-Allow-Origin' header is present on 
  the requested resource. Origin 'null' is therefore not 
  allowed access. The response had HTTP status code 404.

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

尝试2在服务项目的web.config中添加HttpProtocols

foreach ($cells as $cell) { echo ""; } echo "\n"; } fclose($f); ?>

" . htmlspecialchars($cell) . "

答案 1 :(得分:0)

请试试这个......

$.ajax({
type: "GET",
url: "http://localhost/TestingServices/Service1.svc/GetData",
dataType: "jsonp",
success: readData(data),
error: function (xhr, ajaxOptions, thrownError) {
  alert(xhr.status);
  alert(thrownError);
 }
})