由於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;