简体   繁体   English

为什么此Angular脚本无法检索Laravel 4生成的JSON?

[英]Why this Angular script is unable to retrieve JSON generated by Laravel 4?

AngularJS AngularJS

$http({
        method: 'GET',
        url: '//www.ign-design.net/users'
    }).success(function (data, status, headers, config) {
        $scope.user.username = data.name;
    }).error(function (data, status, headers, config) {
        console.log(data);
    }
);

Laravel Laravel

Route::get('users', function() 
{
    return Response::json(array('name' => 'Anil', 'yas' => 25));
});

The problem is, the script works fine when I add //echo.jsontest.com/name/Anil/yas/25 as the $http URL . 问题是,当我将//echo.jsontest.com/name/Anil/yas/25添加为$http URL时,脚本运行良好。 However, then I add //www.ign-design.net/users (which is being generated by Laravel using the code above), it doesn't work. 但是,然后添加//www.ign-design.net/users (由Laravel使用上面的代码生成),它不起作用。 It goes to .error and console.log outputs: "" 转到.errorconsole.log输出: ""

I don't know what's wrong. 我不知道怎么了 Why my Laravel's Response::json() doesn't satisfy AngularJS like jsontest.com website? 为什么我的Laravel的Response::json()不像jsontest.com网站那样满足AngularJS? Spent like 2 hours trying to figure it out - no luck. 花了2个小时试图弄清楚-没运气。

Ps. PS。 Probably a header issue - but I've also tried to output JSON data using plain PHP and setting headers myself. 可能是标头问题-但我也尝试使用纯PHP和自己设置标头来输出JSON数据。 Didn't work either. 也没用。

It appears that you're running into a cross-domain problem. 看来您遇到了跨域问题。 Either move your angular code to the same domain, or make your Laravel code understand how to respond to cross-domain calls. 将您的角度代码移至相同的域,或者使您的Laravel代码了解如何响应跨域调用。

See: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS for more details. 请参阅: https//developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS了解更多详细信息。

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

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