Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

jquery - How to add cores headers in Django ajax call

I tried to hit the API using an ajax call. but it's throwing this Error (from origin 'http://127.0.0.1:8000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request). I add cores headers settings in Django settings and headers also added in ajax call. but I could not get the data throwing the same error.

Ajax Code

$.ajax({                                                                                                
    url: "{{ api.endpoint }}",                                                                          
    type: "GET",                                                                                        
    beforeSend: function(request) {                                                                     
        request.setRequestHeader("Access-Control-Allow-Origin", 'http://127.0.0.1:8000');                                                                               
    },                                                                                                  
    data: {                                                                                             
      "limit" : 1,                                                                                      
      "api_key" : "{{ api.token }}"                                                                     
    },                                                                                                  
    success: function(data) {                                                                           
        alert("Your Connection is successful. Retrieved " + data.length + " records from the dataset!");
    },                                                                                                                                                                                                    
    error: function(err) {                                                                              
        console.log(err)                                                                                
        alert("Connection Failed.");                                                                    
    }                                                                                                   
});                                                                                                     

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...