JSON收到,但“资源解释为脚本但用MIME类型应用程序/ json传输”的错误。 [英] JSON received but error for "Resource interpreted as Script but transferred with MIME type application/json"

查看:821
本文介绍了JSON收到,但“资源解释为脚本但用MIME类型应用程序/ json传输”的错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解析 JSON 文件并将数据添加到 HTML 下拉列表中,代码如下,

  $。getJSON(
http://mobile.icta.lk/services/railwayservice/getAllLines.php?lang=en&jsoncallback=? ,
函数(数据){

var $ s = $('。txtline')。empty();

//这个循环的实际内容(数据中的变量k){
$('< option>< / option>')
.val(数据中的数据)将是
//特定于数据
[k] .value)
.text(data [k] .text)
.appendTo($ s);
}
}

我得到一个错误,

 资源解释为脚本,但使用MIME类型application / json传输。 

getAllLines.php:2Uncaught SyntaxError:意外的标记:


解决方案

 <?php 
//文件网址:

http://localhost/remote-json-proxy.php
$ url ='http://mobile.icta.lk/services/railwayservice/getAllLines.php';
$ qsa ='?';
foreach($ _ GET as $ n => $ v){
if($ n!='callback'){
$ qsa。='&'。 $ n。 '='。 rawurlencode($ V);
}
}
$ json = file_get_contents($ url。$ qsa);
echo sprintf('%s(%s);',$ _GET ['callback'],$ json);
?>

用法:

 <! - 文件url:http://localhost/test.htm  - > 
< script type ='text / javascript'>
$ .getJSON('remote-json-proxy.php?lang = en& this = that& callback =?',function(data){
console.log(data);
})
< / script>


Im parseing a JSON file and add the data to a HTML dropdown , code as follows,

$.getJSON(
    "http://mobile.icta.lk/services/railwayservice/getAllLines.php?lang=en&jsoncallback=?",
    function(data) {

        var $s = $('.txtline').empty();

        // the actual contents of this loop will be
        // specific to the data
        for (var k in data) {
            $('<option></option>')
                .val(data[k].value)
                .text(data[k].text)
                .appendTo($s);
        }
    }
)

Im getting a error as,

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

getAllLines.php:2Uncaught SyntaxError: Unexpected token :

解决方案

Here is a little script:

<?php
    // file url: http://localhost/remote-json-proxy.php
    $url = 'http://mobile.icta.lk/services/railwayservice/getAllLines.php';
    $qsa = '?';
    foreach($_GET as $n => $v) {
        if($n != 'callback') {
            $qsa .= '&' . $n . '=' . rawurlencode($v);
        }
    }
    $json = file_get_contents($url . $qsa);
    echo sprintf('%s(%s);', $_GET['callback'], $json);
?>

Usage:

<!-- file url: http://localhost/test.htm -->
<script type='text/javascript'>
    $.getJSON('remote-json-proxy.php?lang=en&this=that&callback=?', function(data) {
        console.log(data);
    })
</script>

这篇关于JSON收到,但“资源解释为脚本但用MIME类型应用程序/ json传输”的错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆