Access-Control-Allow-Origin in Django app when accessed with Phonegap

前端 未结 3 2097
太阳男子
太阳男子 2020-12-13 02:04

I\'m developing a Phonegap app for my Django based app, but when trying to make Ajax calls I get this error:

XMLHttpRequest cannot load http://domain.herokua         


        
相关标签:
3条回答
  • 2020-12-13 02:44

    For single views you can manually add headers:

    @require_GET
    def api_getto(request):
        response = JsonResponse(
            # your stuff here
        )
        response["Access-Control-Allow-Origin"] = "*"
        response["Access-Control-Allow-Methods"] = "GET, OPTIONS"
        response["Access-Control-Max-Age"] = "1000"
        response["Access-Control-Allow-Headers"] = "X-Requested-With, Content-Type"
        return response
    
    0 讨论(0)
  • 2020-12-13 02:46

    Django by default does not provide the headers necessary to provide cross origin. The easiest way would be to just use this Django app that handles it for you: https://github.com/ottoyiu/django-cors-headers

    You can then set whichever domains you want white listed using the settings

    CORS_ORIGIN_WHITELIST = (
        'google.com',
        'hostname.example.com'
    )
    

    to support allowing all, just use the setting... CORS_ORIGIN_ALLOW_ALL = True and then do any filtering of the request in middleware or in the view.

    0 讨论(0)
  • 2020-12-13 03:00

    In my case I was posting a file more than 1 mb and I was getting this error because of nginx configration (default max size 1 mb) So......

    For me path of nginx.conf was /etc/nginx/nginx.conf.

    In my case I just added client_max_body_size in http block and it worked for me

    http {
        ...
        client_max_body_size 200M;
    }    
    

    Make sure to restart nginx after changing this config

    0 讨论(0)
提交回复
热议问题