2016-05-13 93 views
0

我試圖調用使用AJAXWCF服務,下面是我的代碼:我怎樣才能解決這個錯誤

$.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. 

燦任何人都可以幫我解決這個問題?

+0

刪除的contentType:「應用/ JSON的;字符集= UTF-8 「然後嘗試,,, –

+0

請參閱http://stackoverflow.com/questions/33820142/getting-request-doesnt-pass-access-control-check-no-access-control-allow-orig – RRR

+0

現在,我得到dis錯誤:POST http://localhost/Wcf/service1.svc/GetData 415(無法處理消息,因爲內容t type'application/x-www-form-urlencoded; charset = UTF-8'不是預期的類型'text/xml;字符集= UTF-8' )。 – tiya

回答

0

嘗試2添加HttpProtocols在服務項目的web.config文件

<system.webServer> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
     <add name="Access-Control-Allow-Headers" value="Content-Type" /> 
     <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> 
     </customHeaders> 
    </httpProtocol> 
</system.webServer> 
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); 
} 
}) 
相關問題