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

发布于 2024-08-22 04:00:02 字数 703 浏览 1 评论 0原文

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

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

get '/behavior_count' do
  "60"
end

在浏览器中返回“60”,当尝试通过 jQuery 中的 $.get 访问网站时,它显示为空字符串。奇怪的部分是在 Firebug 中,虽然 Response 为空,但 HTTP 标头正确地将 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

有什么想法吗?

I have a very simple sinatra site that I'm trying to access via ajax through jQuery.

To keep things incredibly simple, this code snippet:

get '/behavior_count' do
  "60"
end

which returns "60" in the browser, shows up as an empty string when attempting to access the site via $.get in jQuery. The strange part is in Firebug, while the Response is empty, the HTTP header correctly describes Content-Length as 2, and I can see the request show up on the server.

Is there something specific to Sinatra that isn't returning my data, or am I not using jQuery correctly?

If it helps, I also tried this code:

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

and my jQuery looks like

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2024-08-29 04:00:02

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

test.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,当然)

Until someone comes up with a proper answer, here's the minimal example I tried and it works:

test.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

(there's also public/js/jquery.js, of course)

落花浅忆 2024-08-29 04:00:02

这可能不是 Sinatra / Jquery 交互问题,而是 Ajax“跨域安全”问题。

您最初的问题可能是由于您的表单和服务器托管在不同的域上。 XMLHTTPRequests,例如.get.post.ajaxForm.ajaxSubmit,如果它们位于不同的位置,将会失败域。检查接收帖子的应用程序上的日志,您可能会在日志文件中看到类似 "OPTIONS Behaviour_count" 404 的内容。基本上,当您直接点击应用程序时,它会起作用,但是当您尝试使用 AJax 来执行此操作,并且 Ajax 正在从不同的域访问它时,即表单上的“action”选项具有 “http ://some. differentdomain.com/behavior_count" 其中。

这可以解释为什么您的简单示例有效,因为表单和帖子发生在同一应用程序/同一域上。

我刚才也遇到了同样的问题五个小时;我想使用通用的“评论”应用程序,并让其他应用程序能够发布到不同域上的一个中央应用程序。这是行不通的。但后来我把这两个应用程序合二为一,一切都很好。或者,您可以尝试使用 JSONP 使其正常工作,同时仍将两个应用程序分开。

我读了“jQuery $.ajax(), $.发布在 Firefox 中将“OPTIONS”作为 REQUEST_METHOD 发送”,这很有帮助。

It's probably not a Sinatra / Jquery interaction problem, instead, it's an Ajax "cross domain security" problem.

Your original problem could be due to the fact that you've got your form and your server hosted on different domains. XMLHTTPRequests such as .get, .post, .ajaxForm and .ajaxSubmit, will fail if they are on different domains. Check your logs on the app that receives the post, and you might see something like "OPTIONS behavior_count" 404 in the log file. Basically, it will work when you hit the app directly, but when you try to use AJax to do it, and that Ajax is accessing it from a different domain, i.e., the "action" option on the form has "http://some.differentdomain.com/behavior_count" in it.

That would explain why your simple example works, because the form and the post are happening on the same application / same domain.

I had the same problem for five hours just now; I wanted to use a generic "comments" application and have other applications be able to post to the one central application, on a different domain. It won't work. But then I made the two applications into one, and everything was fine. Alternately, you can try to use JSONP to make it work and still keep the two applications separate.

I read "jQuery $.ajax(), $.post sending “OPTIONS” as REQUEST_METHOD in Firefox", which helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文