使用 Ext.Ajax.request 进行跨域 Ajax

发布于 2024-12-27 03:52:43 字数 1457 浏览 0 评论 0 原文

看来我无法使用 Ext.Ajax.request 进行跨域 ajax 调用。看起来 ScriptTag: True 没有任何效果。

这是我的代码:

            {
            xtype: 'button',
            text: 'Search',
            ui: 'confirm',
            handler: function() {
                var query = Ext.getCmp("textquery").getValue();
                Ext.Ajax.request({
                    url: 'http://example.com/?search='+query,
                    dataType: 'jsonp',
                    jsonp: 'jsonp_callback',
                    scriptTag: true,
                    success: function(e) {
                        var obj = Ext.decode(e.responseText);
                        var msg = obj;
                        var html = tpl.apply(msg);
                        resultPanel.update(html);
                    }
                });
            }

控制台日志告诉我:

XMLHttpRequest cannot load http://example.com/?search=test&_dc=1326551713063. Origin http://myapp.lo is not allowed by Access-Control-Allow-Origin.

使用jquery我做了同样的事情并且它有效,但我必须使用sencha touch。

              var formData = $("#callAjaxForm").serialize();

              $.ajax({
                url:"http://example.com/leksikonapi/",
                dataType: 'jsonp',
                jsonp: 'jsonp_callback',
                data: formData,
                success: onSuccess,
                error: onError
              });

我看不出两者之间有什么不同。

It seems I can't make a cross domain ajax call with Ext.Ajax.request. It looks like ScriptTag: True doesn't have any effect.

Here is my code:

            {
            xtype: 'button',
            text: 'Search',
            ui: 'confirm',
            handler: function() {
                var query = Ext.getCmp("textquery").getValue();
                Ext.Ajax.request({
                    url: 'http://example.com/?search='+query,
                    dataType: 'jsonp',
                    jsonp: 'jsonp_callback',
                    scriptTag: true,
                    success: function(e) {
                        var obj = Ext.decode(e.responseText);
                        var msg = obj;
                        var html = tpl.apply(msg);
                        resultPanel.update(html);
                    }
                });
            }

The console log tells me:

XMLHttpRequest cannot load http://example.com/?search=test&_dc=1326551713063. Origin http://myapp.lo is not allowed by Access-Control-Allow-Origin.

With jquery I have done the same thing and it works, but I have to use sencha touch.

              var formData = $("#callAjaxForm").serialize();

              $.ajax({
                url:"http://example.com/leksikonapi/",
                dataType: 'jsonp',
                jsonp: 'jsonp_callback',
                data: formData,
                success: onSuccess,
                error: onError
              });

I can't see whats so different between the two.

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

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

发布评论

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

评论(4

轮廓§ 2025-01-03 03:52:43

Sencha Touch 2 的解决方案:使用 Ext.data.JsonP

http://docs.sencha.com/touch/2-1/#!/api/Ext.data.JsonP

Solution for Sencha Touch 2: Use Ext.data.JsonP

http://docs.sencha.com/touch/2-1/#!/api/Ext.data.JsonP

听不够的曲调 2025-01-03 03:52:43

尝试使用 Ext.util.JSONP。我在文档中没有看到使用 Ext.Ajax 执行 jsonp 的方法

Try using Ext.util.JSONP. I don't see a way to do jsonp in the docs using Ext.Ajax

压抑⊿情绪 2025-01-03 03:52:43

是的,没错。它称为同源策略——浏览器不会向除javascript 的来源。如果您控制服务器,则可以使用 CORS 告诉浏览器发出请求。如果您不控制服务器,则必须编写服务器端代理。

yes that's right. It's called the Same Origin Policy -- the browser won't make a request to any domain other than the one from whence the javascript came. If you control the server, you can use CORS to tell the browser to make requests. If you don't control the server, you'll have to write a server side proxy.

ヅ她的身影、若隐若现 2025-01-03 03:52:43

由于 CORS(跨源资源共享),我在 Chrome 上遇到了同样的问题

浏览器将首先发送 OPTIONS 请求,
然后期望返回一些指示允许哪些来源的 HTTP 标头。

我通过在服务器端进行一些设置解决了这个问题
对于 Ruby 和 Node.js 服务器端,现在都运行良好。

Node.js(感谢 文章)

// Enables CORS
var enableCORS = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
        res.send(200);
    }else{
        next();
    }
};
// enable CORS!
app.use(enableCORS);

Ruby(感谢 文章)

class ApplicationController < ActionController::Base
  before_filter :cors_preflight_check
  after_filter :cors_set_access_control_headers

  # For all responses in this controller, return the CORS access control headers.

  def cors_set_access_control_headers
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token'
    headers['Access-Control-Max-Age'] = "1728000"
  end

  # If this is a preflight OPTIONS request, then short-circuit the
  # request, return only the necessary headers and return an empty
  # text/plain.
  def cors_preflight_check
    if request.method == 'OPTIONS'
      headers['Access-Control-Allow-Origin'] = '*'
      headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
      headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Token'
      headers['Access-Control-Max-Age'] = '1728000'

      render :text => '', :content_type => 'text/plain'
    end
  end
end

I got the same problem on Chrome due to CORS (Cross-Origin Resource Sharing)

The browser will first send an OPTIONS request,
then expect to get back some HTTP headers that indicate which origins are allowed.

I've resolved this problem by doing some settings on server side
For both Ruby and Node.js server side, both working well now.

Node.js (Thanks to the essay)

// Enables CORS
var enableCORS = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
        res.send(200);
    }else{
        next();
    }
};
// enable CORS!
app.use(enableCORS);

Ruby (Thanks to the essay)

class ApplicationController < ActionController::Base
  before_filter :cors_preflight_check
  after_filter :cors_set_access_control_headers

  # For all responses in this controller, return the CORS access control headers.

  def cors_set_access_control_headers
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token'
    headers['Access-Control-Max-Age'] = "1728000"
  end

  # If this is a preflight OPTIONS request, then short-circuit the
  # request, return only the necessary headers and return an empty
  # text/plain.
  def cors_preflight_check
    if request.method == 'OPTIONS'
      headers['Access-Control-Allow-Origin'] = '*'
      headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
      headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Token'
      headers['Access-Control-Max-Age'] = '1728000'

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