Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 打印jquery中url传递的值_Javascript_Jquery_Json_Sinatra - Fatal编程技术网

Javascript 打印jquery中url传递的值

Javascript 打印jquery中url传递的值,javascript,jquery,json,sinatra,Javascript,Jquery,Json,Sinatra,这是我的测试 require 'sinatra' require 'json/pure' get '/2015/:teachername/teaching/:subjectname' do content_type :json { "message" => "#{params[:teachername]} teaching #{params[:subjectname]}." }.to_json end 这一切都很好,比如当我通过url localho

这是我的测试

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>


$.getJSON('/2015/:teachername/teaching/:subjectname',函数(数据){
警报(数据、消息);
});


您必须通过在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>

var teachername='Anil';
var subjectname='Sunil';
$.getJSON('/2015/'+teachername+'/teaching/'+subjectname,函数(数据){
警报(数据、消息);
});