django解決ajax跨域請求

由於jquery禁止通過ajax進行跨域請求,所以在通過ajax請求另外一個不同域名服務api的時候會報錯:“No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access”


最簡單的兩個解決辦法:

1、在django中return的時候設置

def xxxxx(request):
        ....
    resp = HttpResponse(json.dumps({'code':code,'msg':msg,'data':data},ensure_ascii=False))
    resp["Access-Control-Allow-Headers"] = "*"
    return resp

2、在nginx中設置

      add_header Access-Control-Allow-Origin *;
      add_header Access-Control-Allow-Headers X-Requested-With;
      add_header Access-Control-Allow-Methods GET,POST,OPTIONS;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章