gpt4 book ai didi

jQuery 获取针对 Sinatra 的请求未获取文本

转载 作者:太空宇宙 更新时间:2023-11-03 17:41:34 25 4
gpt4 key购买 nike

我有一个非常简单的 sinatra 站点,我正尝试通过 jQuery 通过 ajax 访问它。

为了让事情变得非常简单,这个代码片段:

get '/behavior_count' do
"60"
end

在浏览器中返回“60”,在 jQuery 中尝试通过 $.get 访问站点时显示为空字符串。奇怪的是在 Firebug 中,虽然响应为空,但 HTTP header 正确地将 Content-Length 描述为 2,我可以看到请求显示在服务器上。

是否有 Sinatra 特有的东西没有返回我的数据,或者我没有正确使用 jQuery?

如果有帮助,我也试过这段代码:

get '/behavior_count' do
content_type 'text/plain', :charset => 'utf-8'
"60"
end

我的 jQuery 看起来像

$.get('http://mysite:4567/behavior_count'); // Ignore the response, but
// watch the request in firebug

有什么想法吗?

最佳答案

在有人给出正确答案之前,这里是我尝试过的最小示例并且它有效:

测试.rb:

require 'rubygems'
require 'sinatra'

get '/' do
haml :test
end

get '/howmany' do
"42"
end

views/test.haml:

%html
%head
%script{:type => "text/javascript", :src => 'js/jquery.js'}
:javascript
$(document).ready(function() {
$('#btn').click(function(event){
$.get('/howmany', function(data) {
$('#answer').html(data);
});
});
});
%title Test page

%body
%input#btn{:type => 'button', :value => 'How many?'}
#answer

(当然还有public/js/jquery.js)

关于jQuery 获取针对 Sinatra 的请求未获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2275535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com