[英]Origin is not allowed by Access-Control-Allow-Origin
I keep getting this error我不断收到此错误
1Resource interpreted as Script but transferred with MIME type application/json.
1资源解释为脚本,但使用 MIME 类型 application/json 传输。
I am trying to use jquery query to grab my json form a php file on a separate domain here is my php code.我正在尝试使用 jquery 查询从一个单独的域上的 php 文件中获取我的 json 文件,这是我的 ZE134FD7623EZEZ40 代码。
json.php json.php
<?php
header('Content-type: application/json');
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
and here is my jquery这是我的 jquery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var surl = "http://dropp.users35.interdns.co.uk/json.php";
$.getJSON(surl, function(rtndata) {
console.log(rtndata);
});
});
</script>
ok if i change my url to好的,如果我将 url 更改为
http://dropp.users35.interdns.co.uk/json.php?callback= ? http://dropp.users35.interdns.co.uk/json.php?callback= ?
i then get this error?????然后我得到这个错误??????
1Resource interpreted as Script but transferred with MIME type application/json.
I believe the answer to your question involves using JSONP because it involves cross domain scripting...我相信您的问题的答案涉及使用 JSONP,因为它涉及跨域脚本......
It is discussed in a couple of similar questions on StackOverflow: Resource interpreted as script but transferred with MIME type application/json.它在 StackOverflow 上的几个类似问题中进行了讨论: 资源解释为脚本,但使用 MIME 类型 application/json 传输。 & parsererror
& 解析器错误
and here: https://stackoverflow.com/questions/267546/correct-http-header-for-json-file在这里: https://stackoverflow.com/questions/267546/correct-http-header-for-json-file
There is no callback in the returned javascript whether I specify callback=
or not:无论我是否指定
callback=
,返回的 javascript 中都没有回调:
$ curl -v http://dropp.users35.interdns.co.uk/json.php?callback=test_callback
* About to connect() to dropp.users35.interdns.co.uk port 80 (#0)
* Trying 83.170.124.37... connected
* Connected to dropp.users35.interdns.co.uk (83.170.124.37) port 80 (#0)
> GET /json.php?callback=test_callback HTTP/1.1
> User-Agent: curl/7.21.3 (x86_64-pc-linux-gnu) libcurl/7.21.3
> Host: dropp.users35.interdns.co.uk
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 16 Aug 2011 03:04:35 GMT
< Server: Apache
< X-Powered-By: PHP/5.3.2-1ubuntu4.9
< Transfer-Encoding: chunked
< Content-Type: application/json
<
* Connection #0 to host dropp.users35.interdns.co.uk left intact
* Closing connection #0
{"a":1,"b":2,"c":3,"d":4,"e":5}
You must wrap your JSON with a callback if you want it to work:如果您希望 JSON 工作,您必须使用回调包装您的 JSON:
test_callback({"a":1,"b":2,"c":3,"d":4,"e":5});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.