繁体   English   中英

“资源被解释为脚本,但以MIME类型text / html传输”响应

[英]'Resource interpreted as Script but transferred with MIME type text/html' response

我有一个Ajax调用PHP(在另一个域上)登录脚本,该脚本如下所示:

var request = $.ajax({
        crossDomain: true,
        url: "http://www.domain.com/PHP/fct.login.php",
        type: "POST",
        data: $('#loginForm').serialize(),
        dataType: "jsonp",
        contentType: "application/json; charset=utf-8",
    });

在我的PHP脚本中,我有以下内容:

<?php
header('Content-Type: text/javascript');
echo $_GET['callback'] . '(' . "{'status' : '1'}" . "{'text' : 'ok'}" . "{'userid' : $member['user_id']}" . ')';
?>

但这是行不通的。 在我的控制台中,我只是将Resource interpreted as Script but transferred with MIME type text/html消息进行了Resource interpreted as Script but transferred with MIME type text/html ...

编辑 :我更新了我的回答,如“费利克斯·克林(Felix Kling)”所说,但现在我遇到了解析错误。

我更新后的回复如下:

echo $_GET['callback'] . "({status : 1},{text : 'ok'},{userid : ".$member['user_id']."})";

错误看起来像:

Error: Parse error on line 1:
{status : 1},{text :
-^
Expecting 'STRING', '}'

编辑2 :没关系,它现在可以使用(更新后的响应)。 当我通过浏览器直接查看php脚本时(没有ajax调用等),我只是遇到了parse error

您生成的JS代码不正确。 看起来像

callbackName({'status': '1'}{'text' : 'ok})

但是您不能将对象字面量放在后面。

您要么生成一个文字:

callback({status: 1, text: 'ok'})

或多个参数,以逗号分隔:

callback({status: 1}, {text: 'ok'})

尽管我认为jQuery期望仅将一个参数传递给回调。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM