开发者

XMLHttpRequest to Rack application

开发者 https://www.devze.com 2023-02-04 11:11 出处:网络
I can\'t seem to get XMLHttpRequest to work with my Rack application. I\'ve only recently discovered Rack so please let me know if I\'m doing something incorrectly. I\'ve boiled my code down to just t

I can't seem to get XMLHttpRequest to work with my Rack application. I've only recently discovered Rack so please let me know if I'm doing something incorrectly. I've boiled my code down to just the basic interaction, which doesn't seem to work:

XMLHttpRequest:

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:9292", true);
xhr.send();
xhr.onreadystatechange = function() {
    if(xhr.readyState == 4) {
      alert(xhr.status);
    }
}

Ruby code (config.ru):

run lambda { |env|
    response = Rack::Response.new("testing");
    response.finish
}

After running rackup config.ru, when I point my browser to http://localhost:9292, everything seems to work; however, the above XMLHttpRe开发者_StackOverflow社区quest returns "0" as the response status, even though WEBrick shows 200 as the status. I don't get any responseText.

Any ideas why this is happening? What am I missing?


Silly me, you can't have cross-domain XMLHttpRequests (my original script was running on port 80 but Ruby was on port 9292).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号