Ajax Post Cross Domain 跨域請求 WCF RestFull,wcfrestfull

來源:互聯網
上載者:User

Ajax Post Cross Domain 跨域請求 WCF RestFull,wcfrestfull

前段時間做手機WebAPP, 但開發人員習慣在瀏覽器上先調試準系統, 但這裡就出現了跨域請求問題

當然如果你自己寫服務, 自己寫WebAPP 都是localhost 就不會跨域, 而且發布到手機上也不會跨域


 關鍵來了!!!!

1. 先要確保你的js寫的是對的

 $.ajax({        url: url3,        data: JSON.stringify({userName:uid,userPass:pwd}),                   contentType:"application/json; charset=utf-8",                   type:"POST",                   crossDomain: true,                   dataType: 'json',        success: function (data) {        },        error: function (xhr, textStatus, errMsg) {                    }});

2. 確保你的服務支援OPTION 請求格式, 因為Jquery 跨域請求好像會請求兩次, 第一次OPTION, 第二次POST , 所以你的Method上面應該寫 * ,而不是POST



3. 你的web.config 的 system.webServer 節點需要增加跨域響應支援

<httpProtocol>

      <customHeaders>

        <addname="Access-Control-Allow-Origin"value="*" />

        <addname="Access-Control-Allow-Headers"value="Content-Type" />

        <addname="Access-Control-Allow-Methods"value="GET, POST,PUT, DELETE, OPTIONS" />

      </customHeaders>

</httpProtocol>


4.這個不知道是不是必須的, 需要在system.serviceModel 中的standardEndpoint增加

crossDomainScriptAccessEnabled="true"


本人花了6個小時才解決,希望其他人少走彎路



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.