1、服務端:
web.config文件中:
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="http://nmcs.lemon95.com:8089" /> <add name="Access-Control-Allow-Credentials" value="true" /> <add name="Access-Control-Allow-Headers" value="SecretKey,AppKey,UniqueKey" /> </customHeaders> </httpProtocol> </system.webServer>
ps:
Access-Control-Allow-Origin:只能配置一個域名,不能配置多個域名,不能為"*"
Access-Control-Allow-Headers:加入需要傳入參數名
2、客服端ajax異步調用
<script src="js/jquery-1.8.2.js"></script> <!--<script src="js/stats.js"></script>--> <script> function test() { var url = "http://xxxx/Stats"; //api地址 $.ajax({ type: "get", url: url, data: null, dataType: "json", cache: false, xhrFields: { withCredentials: true }, crossDomain: true, headers: { SecretKey: "ec8b570ad4bd403783c52ecb5cdfa849", AppKey: "1259402909", UniqueKey:"987654321" }, beforeSend: function () { }, success: function (data) { alert(data); }, complete: function (XMLHttpRequest, textStatus) { }, error: function (errorData) { alert(errorData); } }); } </script>