javascript - Cordova本地ajax请求报错

标签 javascript ajax cordova laravel

我正在尝试从本地主机上的 PHP 脚本检索信息

app.js 在 Cordova 应用程序上:

var url = 'http://localhost:8000/locations';

$.ajax({
   url: url,
      type: 'GET',
      contentType: "application/json",
      async: true,
      dataType: 'jsonp',
      crossDomain: true,
      success: function(resp){
          console.log(resp);
      },
      error: function(err) {}
});

和 php 代码(使用 Laravel 框架)

return Location::all()->toJson();

我有这个错误

Refused to load the script 'http://localhost:8000/locations?callback=jQuery21309354114597663283_1431278135791&_=1431278135792' because it violates the following Content Security Policy directive: "default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

最佳答案

您需要将策略添加到您的 Cordova 应用。

http://content-security-policy.com/

http://www.html5rocks.com/en/tutorials/security/content-security-policy/

第二个链接正是你所需要的,文章写得很好我只能引用:

https://apis.google.com/js/plusone.js in the context of this page’s origin. We trust that code, but we can’t expect the browser to figure out on it’s own that code from apis.google.com is awesome, while code from apis.evil.example.com probably isn’t. The browser happily downloads and executes any code a page requests, regardless of source.

Instead of blindly trusting everything that a server delivers, CSP defines the Content-Security-Policy HTTP header that allows you to create a whitelist of sources of trusted content, and instructs the browser to only execute or render resources from those sources. Even if an attacker can find a hole through which to inject script, the script won’t match the whitelist, and therefore won’t be executed.

关于javascript - Cordova本地ajax请求报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30163284/

相关文章:

javascript - 为什么 PhoneGap Android 应用程序在将大量数据插入 SQL 时崩溃?

javascript - 使用 ng-repeat 从集合中提取一些信息

javascript - 如何使用 JavaScript 创建 session ?

javascript - 有效 json 的 jQuery 语法错误

javascript - 停止JS中的长轮询功能

Android 应用程序在相机打开并拍照时重新启动

cordova - Visual Studio Cordova 模板/AngularJSTodo 示例项目调试问题

javascript - Angular ng-select : selectedItems. map 不是函数

javascript - 通过单击另一个按钮撤消按钮动画,但前提是动画已播放

javascript - Internet Explorer 不会重定向 ajax 响应