该CSP不能引起你所描述的问题。您很有可能使用的是JSONP而不是纯JSON。JSONP在Chrome中不起作用,因为JSONP通过
<script>在文档中插入标签而起作用,该标签的
src属性设置为Web服务的URL。CSP不允许这样做。
前提是您已在清单文件中设置了正确的权限(例如
"permissions":["http://domain/getjson*"],您将始终能够获取和解析JSON:
var xhr = new XMLHttpRequest();xhr.onload = function() { var json = xhr.responseText; // Response json = json.replace(/^[^(]*(([Ss]+));?$/, '$1'); // Turn JSONP in JSON json = JSON.parse(json); // Parse JSON // ... enjoy your parsed json...};// Example:data = 'Example: appended to the query string..';xhr.open('GET', 'http://domain/getjson?data=' + enpreURIComponent(data));xhr.send();在将jQuery用于Ajax时,请确保不通过
jsonp: false以下方式请求JSONP :
$.ajax({url:'...', jsonp: false ... });或者,当使用时
$.getJSON:
$.getJSON('URL which does NOT contain callback=?', ...);


