这是我的test.rb
require 'sinatra'
require 'json/pure'
get '/2015/:teachername/teaching/:subjectname' do
content_type :json
{
"message" => "#{params[:teachername]} teaching #{params[:subjectname]}."
}.to_json
end
一切都很好,比如当我通过url localhost:4567/2015 / Anil / teaching / Sunil访问时,但是当我在jquery中访问这个url时,我无法通过:teachername和:subjectname。
这是我的test.html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$.getJSON('/2015/:teachername/teaching/:subjectname', function(data) {
alert(data.message);
});
</script>
答案 0 :(得分:0)
您必须通过在Javascript中附加参数来构建URL字符串。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
var teachername = 'Anil';
var subjectname = 'Sunil';
$.getJSON('/2015/'+teachername+'/teaching/'+subjectname, function(data) {
alert(data.message);
});
</script>