Ext 例子不能從伺服器讀到json資料的問題

來源:互聯網
上載者:User

錯誤:

最近在研究 django+extjs 的使用,我按照 ext 的例子,用js建了grid ,並取 django 伺服器中的資料,但總是出現下面錯誤,取不到資料.

XMLHttpRequest cannot load http://127.0.0.1:8000/test/?_dc=1285778542180. Origin null is not allowed by Access-Control-Allow-Origin.

原因:
根據錯誤提示,google 了一下,找到了原因,因為我是直接開啟本地 html 檔案,然後在 html 檔案裡的js來取 django服務資料,這被似作是跨域訪問了,當跨域 ajax 訪問時,clinet 需要得到伺服器的許可,方式是通過取得伺服器送回的特殊 head 值來判斷的,只要我們讓服務端返回這幾個頭變數值就行了.

解決: (django 服務端修改一下)

    res=HttpResponse()

    res['Access-Control-Allow-Origin']='*'

    res['Access-Control-Allow-Headers']='my-header,X-Requested-With'

    res.write('{"rows":[{....},{....}......]}')

    return res

 

 

參考 w3c 的文獻:

http://www.w3.org/TR/2010/WD-cors-20100727/#introduction

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.