简体   繁体   English

跨域Ajax呼叫Jquery

[英]Cross Domain Ajax Call Jquery

I am trying to call a page with ajax, but from another server. 我正在尝试使用ajax调用页面,但是是从另一台服务器调用的。 This js code runs from bookmark in any page you want and I need to get some data from my server. 此js代码可从任何所需页面的书签中运行,我需要从服务器中获取一些数据。 How could I do this? 我该怎么办? or maybe connect directly to the database? 还是直接连接到数据库?

I got this code: 我得到以下代码:

$j.getJSON(serverUrl+"isLogin.php?callback=?",function(data){
 //really no need to do anything here, we're just posting data
 //but this will output success
 alert(data);
});

isLogin: isLogin:

if ($_SESSION['user'] == ''){
     echo json_encode(array("isLogin" => "false"));

} else {
    echo json_encode(array("isLogin" => "true"));
}

How could I make this? 我该怎么做? I try allot of solutions but none works. 我尝试分配解决方案,但无济于事。 This one said no errors, but the alert never appear 这说没有错误,但警报从未出现

Thanks 谢谢

AJAX (The XMLHttpRequest object) is limited to the same domain for security reasons. 出于安全原因,AJAX(XMLHttpRequest对象)仅限于同一域。

You have a couple ways to go about this: 您有几种方法可以解决此问题:

You can use JSONP if your API supports it. 如果您的API支持,则可以使用JSONP。 This is very similar to your current AJAX approach. 这与您当前的AJAX方法非常相似。 This is jQuery's documentation - http://api.jquery.com/jQuery.getJSON/ 这是jQuery的文档-http://api.jquery.com/jQuery.getJSON/

Another option is to create a local proxy. 另一种选择是创建本地代理。 This is a little more in depth, but gives you full control. 稍微深入一点,但可以完全控制。 Here's a good article - http://developer.yahoo.com/javascript/howto-proxy.html 这是一篇好文章-http://developer.yahoo.com/javascript/howto-proxy.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM