相关疑难解决方法(0)

jQuery AJAX跨域

这是两个页面,test.php和testserver.php.

test.php的

<script src="scripts/jq.js" type="text/javascript"></script>
<script>
    $(function() {
        $.ajax({url:"testserver.php",
            success:function() {
                alert("Success");
            },
            error:function() {
                alert("Error");
            },
            dataType:"json",
            type:"get"
        }
    )})
</script>
Run Code Online (Sandbox Code Playgroud)

testserver.php

<?php
$arr = array("element1",
             "element2",
             array("element31","element32"));
$arr['name'] = "response";
echo json_encode($arr);
?>
Run Code Online (Sandbox Code Playgroud)

现在我的问题是:当这两个文件都在同一台服务器(localhost或web服务器)上时,它可以工作并被alert("Success")调用; 如果它位于不同的服务器上,意味着Web服务器上的testserver.php和localhost上的test.php,它就无法工作,并且alert("Error")正在执行.即使ajax中的URL更改为http://domain.com/path/to/file/testserver.php

javascript ajax jquery json cross-domain

468
推荐指数
10
解决办法
69万
查看次数

Facebook Graph API使用Ajax获取JSON

这是我第一次在jQuery中使用Facebook.

我收到以下错误:

Uncaught SyntaxError: Unexpected token : 
www.facebook.com/feeds/page.php?id=20531316728&format=JSON
    &callback=jQuery110105899784066714346_1383828332964&_=1383828332965:2
Run Code Online (Sandbox Code Playgroud)

$.ajax({
    url: 'http://www.facebook.com/feeds/page.php?id=20531316728&format=JSON',
    dataType: 'jsonp'
}).done(function(data) {
    alert(data);
});
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

我为什么要这个?

ajax jquery json facebook facebook-graph-api

5
推荐指数
1
解决办法
5658
查看次数