I have this code in my create.js.erb
file:
pollingAJAX();
function pollingAJAX() {
$.ajax({
method: "POST",
url: "/status",
data: {uuids: '<%= @uuid_hash.to_json %>'},
},
success: function(data){
var obj = $.parseJSON(data);
if(obj.isDone == "yes"){
}else{
obj.each(function(result) {
if(result.status == "completed"){
$('a[href="#{result.开发者_如何学Pythonurl}"]').html('');
}
});
pollingAJAX();
}
}
});
}
This AJAX request is not being triggered because my create.js.erb file is not being rendered. I have this code in my create
action:
result['items'].each do |r|
# escaped_url = URI.escape(r['link'], Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")).gsub("%","-")
@site_array << r['link']
if Result.where(:link => r['link']).present?
else
job_id = ImageGenerator.create(:url => r['link'])
@uuid_hash[r['link']] = job_id
end
end
respond_to do |format|
format.html
format.js
end
I think it may have to do with the fact that I am processing a bunch of background jobs with Resque before calling respond_to
?
How can I get my create.js.erb
file to be triggered?
UPDATE: Make sure you're actually including your JavaScript with a <script>
tag, otherwise it will never be loaded.
Move the invocation of pollingAJAX()
to after that function has been defined. Provided it (lexically) parses ok, JavaScript executes linearly, as it's interpreted, rather than compiled and then executed, so the pollingAJAX()
function doesn't yet exist where you've put the invocation.
Also, always view your source code in the browser, to make sure the .erb has processed the way you intended. And make use of your JavaScript console in your browser to detect any errors.
Since it appears that you are using jQuery from your AJAX request syntax, you can bind your method to the DOM load event:
$(function() {
pollingAJAX();
});
This will prevent pollingAJAX() from executing until after the entire Javascript file has been parsed, allowing this function to find its definition.
See more here: http://www.learningjquery.com/2006/09/introducing-document-ready
精彩评论