Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSON响应项未定义_Javascript_Json_Undefined_Response - Fatal编程技术网

Javascript JSON响应项未定义

Javascript JSON响应项未定义,javascript,json,undefined,response,Javascript,Json,Undefined,Response,我在显示JSON响应的文本时遇到问题: 我有以下回应: { "status": false, "msg": "Achtung: Zeile 2 enthält ungültige Zeichen!", "csv_data": [] } 现在,我想在控制台中记录“msg”文本,以便进行测试。 这就是我尝试过的: [...], success: function (file, response) { // this response works and logs the

我在显示JSON响应的文本时遇到问题: 我有以下回应:

{
  "status": false,
  "msg": "Achtung: Zeile 2 enthält ungültige Zeichen!",
  "csv_data": []
}
现在,我想在控制台中记录“msg”文本,以便进行测试。 这就是我尝试过的:

[...], success: function (file, response) {
        // this response works and logs the above JSON
        console.dir(response);

        // the following return "undefined"
        console.log(response.msg);
这里有什么问题?我找不到毛病。。。
谢谢你的帮助!:)

可能是因为它被串起来了。尝试:

JSON.parse(response).msg
如果不起作用,请尝试:

response.data.msg

响应的类型为string。您必须将其解析为json:

let response='{“status”:false,“msg”:“Achtung:Zeile 2 enthält ungültige Zeichen!”,“csv_数据”:[]}
让json_response=json.parse(response);

log(json_response.msg)如果响应不是对象,则解析响应,否则不执行任何操作
response=typeof response==“object”?response:JSON.parse(response)
按照


试试
typeof response
,我打赌它不是一个对象,您可能需要对它进行反序列化first@CertainPerformance是的,我也觉得响应是字符串…@Philipp尝试使用JSON.parse(response)反序列化然后check@CertainPerformance我实际上也在想,这不是预期的结果,但我假设它实际上可能是一个数组-
[{“status”:false,“msg:“Achtung:Zeile 2 enthält ungültige Zeichen!”,“csv_data:[]}]
谢谢大家的快速回答!可悲的是,到目前为止,一切都无济于事@CertainPerformance
typeof
返回“string”@Philipp如果typeof返回string,那么解析应该可以工作。。发布您的实际响应字符串
const finalResponse = typeof response === 'object'? response: JSON.parse(response);
console.log(finalResponse.msg);