pull json data from another service provider and import them into database
I have a rails application, using OmniAuth to connect with other service providers.
My application have User model and Task model, user has many tasks, and task belongs to user.
After user log into the application, I set the view redirect to the task list index.
I'm expecting when redirecting, jQuery will call Ajax getJSON f开发者_如何学JAVAunction and get json data from provider, and store them into my database. I have put following code into my application.js
$.ajaxSetup({
dataType: 'json',
beforeSend: function(xhr){
xhr.setRequestHeader('Authorization', 'OAuth '+token);
},
contentType: "application/json; charset=utf-8"
});
Also I have add one js file called task.js, and include this with task index view. With in the task.js
function GetTasks(){
$.getJSON(url, function(data){
console.log(data);
});
}
$.ready(function(){
GetTasks();
});
Questions: seems my $.ajaxSetup setting never took place when loading the task index view. How should I modify make my idea happen? Thank you so much.
-------UPDATE------- I've added flowing code into my application_controller after_filter :set_access_control_headers
def set_access_control_headers
headers['Access-Control-Allow-Origin']='*'
headers['Access-Control-Request-Method']='*'
end
I'm expecting when redirecting, jQuery will call Ajax getJSON function and get json data from provider, and store them into my database. I have put following code into my application.js
I'm not quite clear what is going on but I think what you are trying to do violates the same source
rule for ajax requests. For security reasons you may only make async requests to the same domain the page loaded from. If this is not the case please clarify your question.
精彩评论