【问题标题】:AngularJS http.jsonp request .then and .success functions not workAngularJS http.jsonp 请求 .then 和 .success 函数不起作用
【发布时间】:2016-08-03 14:40:19
【问题描述】:

我的客户端应用程序是一个 angularJS 应用程序。服务器由 Laravel 5 构建。

在客户端,我进行 jsonp CORS 请求:

$http.jsonp("http://server.com/api?callback=JSON_CALLBACK")

在laravel服务端,返回一个json数据:

return Response()->json($query);

但是,当我尝试时

$http.jsonp("http://server.com/api?callback=JSON_CALLBACK")
.then(function(response){
    alert('success');
});

$http.jsonp("http://server.com/api?callback=JSON_CALLBACK")
.success(function(response){
    alert('success');
});

两种状态都是200 OK。但不会出现任何警报。我试试

$http.jsonp("http://server.com/api?callback=JSON_CALLBACK")
.then(function(response){
    alert('success');
},function(error){
    alert('fail');
});

结果是200 OK 的状态,但警报是“失败”!我怎么解决这个问题?谢谢!

【问题讨论】:

    标签: javascript php angularjs laravel jsonp


    【解决方案1】:

    JSONP 需要服务器端的回调函数“JSON_CALLBACK”。或者,您可以使用代理。 http://www.whateverorigin.org/

    【讨论】:

      【解决方案2】:

      Angular 的$http.jsonp 将请求查询字符串参数从callback=JSON_CALLBACK 转换为callback=angular.callbacks._0

      在 Laravel 的控制器中,您需要在响应字符串中添加前缀。

      return $request->input('callback')."(".$query.")";
      

      然后,Angular 应用程序将确认此响应并传递给.then.success 函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-04
        • 1970-01-01
        • 2016-02-12
        • 2016-05-26
        • 1970-01-01
        • 2018-07-22
        • 2013-06-18
        • 1970-01-01
        相关资源
        最近更新 更多