与阿贾克斯(JQuery的)跨域问题域我自己 - 简单的PHP页面 [英] Cross domain issue with .ajax (JQuery) to a domain I own -- simple PHP page

查看:124
本文介绍了与阿贾克斯(JQuery的)跨域问题域我自己 - 简单的PHP页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道这是一个普遍的问题,已经看到有很多人在谈论它,但觉得我的情况是独一无二的,也许还不如涉及其他。

So I know this is a common problem and have seen a lot of people talking about it but feel that my situation is unique and maybe not as involved as others.

我有一台主机的网站,并试图建立与JQ和PhoneGap的iPhone应用程序。

I have a host website and trying to build an iPhone app with JQ and PhoneGap.

我希望人们能够自己得分传递给从App我的家域,但我得到了可怕的: 原产地空没有被访问控制 - 允许 - 产地允许的。 当我尝试把这个:

I want people to be able to pass their score to my home domain from the app but am getting the dreaded: "Origin null is not allowed by Access-Control-Allow-Origin." when I try and call this:

$.ajax({
        type: "POST",
        url: 'http://www.homesite.com/thephppage.php',
        data: {
            'guid': '12333-54',
            'score': 52,
            'initials': 'tod'
        },
        success: function (data) {
            try {

                }
                else {

                }
            }
            catch (err) {
                alert(err);
            }
        },
        error: function (xhr) {
            alert(xhr.responseText);
        }
    });

由于这是在呼唤我自己的域名,是有什么简单的,我可以做些什么来纠正这个问题呢?

Since this is calling out to a domain I own, is there something simple I can do to rectify this issue?

感谢。 牛逼

推荐答案

工作! OH光荣! (排序):这是我做的事......

WORKING!!! OH GLORY!!! (sort of): Here's what I did...

相反,我去了JSONP路径(在某种程度上),并新增了标签我的头动态像这样:

Instead of use the .ajax, I went the JSONP route (sort of) and added a tag to my head dynamically like so:

var head = document.getElementsByTagName('head')[0];
  script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'http://thesite.co/thepage.php?var1=123&var2=432&var3='ttt';
  head.appendChild(script);

我假设没有给它一个回调方法,因为(因为我自己的目标网站,并知道它的到来)对我的PHP身边,我换了回报与这样的方法名:

I didnt even have to give it a CALLBACK method because (since I own the target site and know its coming) on my PHP side, I wrap the return with a methodName like this:

$ EN codeD = json_en code($ JSON); 回声TestMethod的($ EN codeD。); 这不平凡的工作,除非我的DARN iPhone和iPad模拟器! (天哪,我需要买真正的设备......)所以我还是不知道如果这个工程上的实际设备,但现在它在Chrome和Safari ......让希望我可以计算出如何使在该电话的工作!

$encoded = json_encode($json); echo "testMethod(" . $encoded . ")"; This worked marvelously EXCEPT IN MY DARN iPhone and iPad simulators!!! (gosh I need to buy the real devices...) So I STILL dont know if this works on the actual devices but it works now in Chrome AND Safari... lets hope I can figure out how to make it work on that phone!

感谢大家的帮助!

==========第二更好的移动解决方案======================这工作比JSONP好 - 动态脚本的解决方案。 ..

==========SECOND BETTER MOBILE SOLUTION====================== This works better than the JSONP - dynamic Script solution...

在您下的PhoneGap和JQ调用你的头插入此标记。

In your head under your Phonegap and JQ calls insert this tag.

<script type="text/javascript">
        $(document).bind("mobileinit", function () {
           $.mobile.allowCrossDomainPages = true;
        });
    </script>

然后在你的X code资源文件夹中的文件Phonegap.plist,域加入到外部域...即。 phonegap.com。 (离开过HTTP或www)。

Then in your Phonegap.plist file in your XCODE Resources folder, add the domain to External Domains... ie. phonegap.com. (Leave off http or www).

这篇关于与阿贾克斯(JQuery的)跨域问题域我自己 - 简单的PHP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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