在PhoneGap / Cordova中没有“Access-Control-Allow-Origin”,以及最新版本的Google Chrome [英] No 'Access-Control-Allow-Origin' in PhoneGap/Cordova, as well as the latest versions of Google Chrome

查看:1381
本文介绍了在PhoneGap / Cordova中没有“Access-Control-Allow-Origin”,以及最新版本的Google Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Sencha Touch Framework 的跨平台工作,我在 mac os x localhost ,我想通过 Ext.Ajax.request 请求服务器的响应。我收到一个典型的错误 CORS: XMLHttpRequest无法加载没有访问控制允许原因无访问控制允许 - Origin头部存在于所请求的资源上。因此,'http:// localhost'不允许访问。该响应有HTTP状态代码403
如何解决我的问题,我在哪里需要把头信息放在服务器上。

I'm working in Sencha Touch Framework of cross mobile platform, I'm running my project in mac os x localhost, I'm trying to get the server's response from requesting through Ext.Ajax.request. I'm getting a typical error of CORS: XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 403 How can I resolve my issue and where exactly I need to put the headers information on server.

header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Headers: X-Requested-With'); 
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');

这些都是我需要在我的服务器上的头,但我仍然无奈,服务器。我请求的网站是在Wordpress,我尝试了 jt-cross-domain-ajax 插件来解决我的问题,但我不能这样做。

These are the headers I need on my server but I'm still helpless and having the same error from server. My requested website is in Wordpress and I tried jt-cross-domain-ajax plugin to resolve my probelm but I'm unable to do this.

我的服务器代码

    add_action('wp_ajax_fun', 'myFunc');
    add_action('wp_ajax_nopriv_fun', 'myFunc');
    function myFunc()
    {
// Here I'm currenlty putting the header informatioins
        wp_send_json_success("I'm Working");
        die();
    }

我的Sencha Touch代码

 Ext.Ajax.request({
                url: URL, // Some url
                method: 'GET',
                callback: function(options, success, response) {
                    Ext.ComponentQuery.query('#id')[0].setHtml( response.responseText );
                }
            });

我的问题是如何解决我的问题,我应该把我的信息头文件

My Question is how can I resolve my problem and where I should put my information of headers origins and what are the steps to achieve this goal.

推荐答案

您的Sencha Touch应用程式将在实际的PhoneGap / Cordova应用程式中正常运作。通常这种问题可以通过降低桌面浏览器的安全级别来解决。

Your Sencha Touch app will run without issues in actual PhoneGap / Cordova application. Usually this kind of issue can be resolved by lowering down security level of your desktop browser.

但是,如果你真的想在桌面环境中模拟你的项目,方法)以下是Google Chrome浏览器的部分步骤:

However if you really want to emulate your project on your desktop environment, (my favorite method) here are some steps with Google Chrome browser:


  • 完全退出Chrome浏览器。

  • 打开您的终端。只需输入此命令即可。


open / Applications / Google\ Chrome.app/ --args
--disable-web-security

open /Applications/Google\ Chrome.app/ --args --disable-web-security




  • 重新运行Chrome。如果Chrome安全功能已成功停用,您应该会在Chrome视图顶部看到黄色警告栏。

    • Re-run your Chrome. If chrome security is successfully disabled, you should see yellow warning bar at the top of the chrome view.

      打开您的Sencha Touch项目,

      Open your Sencha Touch project by opening its index.html

      更新:

      最近在Mac OSX中的Chrome浏览器更新(版本49.0.2623.87)仍然给我CORS问题,即使我运行通过上述命令。我不得不指定--user-data-dir另外为了克服它。现在应该是:

      Recent Google Chrome updates (version 49.0.2623.87) in Mac OSX still gave me CORS issue, even though I ran through above command. I had to specify --user-data-dir additionally in order to overcome it. Now it should be:


      open / Applications / Google\ Chrome.app/ --args
      --disable -security - user-data-dir =〜/ ChromeUserData /

      这篇关于在PhoneGap / Cordova中没有“Access-Control-Allow-Origin”,以及最新版本的Google Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆