简体   繁体   中英

Respond with JavaScript in Rack app

I have a Rack app where I need to respond with JavaScript. I am trying set content type as JavaScript, while sending the response but its not working, it seems like I am missing somthing.

The simple response I am sending is:

require 'rubygems'   
require 'rack'  
run Proc.new { |env| ['200', {'Content-Type' => 'application/javascript'},["alert('hi')"]]} 

Can a Rack app respond with a JavaScript content-type? Or could it be the browser is not executing the received JavaScript?

as @matt said above, and what I was trying to figure out is that you can't just give the browser js and expect it to run. It has to be executed by an ajax callback. Take jQuery for example:

$.ajax({
  url: "/path/to/your/js",
  dataType: "script"
});

That dataType property tells jQuery to execute the script that is returned by the server. The point being that you need javascript to execute javascript, simply navigating to a URL won't do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM