Access-Control-Allow-Origin error when getting Wordpress posts in Phonegap app

后端 未结 4 1394
南笙
南笙 2021-02-09 22:42

Hopefully someone can help me to resolve the following problem while developing a mobile app in phonegap. I am attempting to read posts from a wordpress installation but I get t

相关标签:
4条回答
  • 2021-02-09 23:21

    So the problem that you are running into is commonly referred to as Cross Origin Security. Basically, most web-browsers will not allow you to pull in content from servers outside your own, unless the server says it is ok. To do this, the server needs to see an acceptable Access-Control-Allow-Origin in the headers.

    The good news is that this is fairly easy to fix, as Bowdenweb points out in How to Enable cors in WordPress.

    You only need to add the appropriate header to the headers.php file, like so

    <?php /** @package WordPress @subpackage Default_Theme  **/
    header("Access-Control-Allow-Origin: *"); 
    ?>
    <!DOCTYPE html>
    

    Update 1

    As ILI pointed out, there is a plugin for Wordpress called WordPress-Cross-Domain-Plugin which resolved this issue for him.

    0 讨论(0)
  • 2021-02-09 23:36

    Browsers don't allow cross domain calls in ajax by default. Jamie Starke gives you the answer. Maybe try to use $.support.cors = true; with JQuery. Anyway, a Phonegap app can do ajax cross domain requests. If you can't, check your config.xml http://docs.phonegap.com/en/edge/guide_whitelist_index.md.html

    0 讨论(0)
  • 2021-02-09 23:38
    in plist file add
    
     <key>NSAppTransportSecurity</key>
    
    
    <dict>
        <key>NSExceptionDomains</key>
             <dict>
                 <key>yourdominename.com</key>
                        <dict>
                          <!--Include to allow subdomains-->
                          <key>NSIncludesSubdomains</key>
                          <true/>
                          <!--Include to allow HTTP requests-->
                          <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                          <true/>
                          <!--Include to specify minimum TLS version-->
                          <key>NSTemporaryExceptionMinimumTLSVersion</key>
                          <string>TLSv1.1</string>
                    </dict>
              </dict>
    </dict>
    

    in config file

     <preference name="BackupWebStorage" value="none" />
    
    0 讨论(0)
  • 2021-02-09 23:41

    Those who are still having problems with Access-Control-Allow-Origin despite having wildcarded their config.xml might like to check this out if they're using Jellybean. From what I can gather it is fixed in the newest version of phonegap but installation of the newest version is beyond my limited talents!

    Read this: Phonegap/Cordova App breaks in Jelly Bean - Access-Control-Allow-Origin and setAllowUniversalAccessFromFileURLs

    0 讨论(0)
提交回复
热议问题