Access-Control-Allow-Origin不允许使用Origin

时间:2011-08-15 16:12:16

标签: jquery json

我一直收到此错误

  

1Resource解释为脚本,但使用MIME类型application / json进行传输。

我正在尝试使用jquery查询来抓取我的json表单在一个单独的域上的php文件这里是我的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);
?>

这是我的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>

好的,如果我将我的网址更改为

http://dropp.users35.interdns.co.uk/json.php?callback=

然后我得到这个错误?????

1Resource interpreted as Script but transferred with MIME type application/json.

2 个答案:

答案 0 :(得分:1)

我相信你的问题的答案涉及使用JSONP,因为它涉及跨域脚本......

在StackOverflow上的几个类似问题中讨论了它: Resource interpreted as script but transferred with MIME type application/json. & parsererror

在这里: https://stackoverflow.com/questions/267546/correct-http-header-for-json-file

答案 1 :(得分:0)

返回的javascript中没有回调,无论我是否指定callback=

$ 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}

如果您希望它能够工作,您必须使用回调包装JSON:

    test_callback({"a":1,"b":2,"c":3,"d":4,"e":5});