[英]Issue in converting received ajax data
Ajax is used to retrieve data from server. Ajax用于从服务器检索数据。
Data is displayed successfully on alert as 数据在警报时成功显示为
data: {"status": "Active","broadcastmsg": "msg"}
But in html ie <p id="demo"></p>
it is displayed as undefined . 但是在html即
<p id="demo"></p>
它显示为未定义 。
Looks like a minor issue , though I have been trying to solve this since last 24 hours and nothing seems to be working. 看起来像一个小问题,虽然我一直试图解决这个问题,因为过去24小时似乎没有任何工作。 Please suggest what's going wrong.
请说明出了什么问题。
Client.php
Client.php
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<body>
<script>
function myFunction(val) {
$.post("test.php",
{
d_id: 1,
date: val
},
function(hstatus){
alert(hstatus);
myJSON = JSON.stringify(hstatus);
localStorage.setItem("testJSON", myJSON);
//Retrieving data:
text = localStorage.getItem("testJSON");
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.status;
});
}
</script>
<p id="demo"></p>
<div id="future_date" style="display:block;" class="col-xs-12">
<input type="date" name="txt" value="" onchange="myFunction(this.value)">
</div>
</body>
</html>
EDIT
编辑
if we pass hstatus = {"status": "Active","broadcastmsg": "msg"}
manually it's showing result. 如果我们通过
hstatus = {"status": "Active","broadcastmsg": "msg"}
手动显示结果。
Thus issue is in converting from 因此,问题在于转换
data: {"status": "Active","broadcastmsg": "msg"}
to 至
{"status": "Active","broadcastmsg": "msg"}
Update: 更新:
If you have the "data: " part in front of the JSON data, you have to trim: 如果您在JSON数据前面有“data:”部分,则必须修剪:
myJSON = hstatus.substring(6);
see: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substring 请参阅: https : //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substring
Option A: You should include one more ( dataType
) parameter in the $.post
call, so the jQuery knows the content type, otherwise you will get the response as plain text. 选项A:您应该在
$.post
调用中包含一个( dataType
)参数,因此jQuery知道内容类型,否则您将以纯文本形式获得响应。 https://api.jquery.com/jquery.post/ https://api.jquery.com/jquery.post/
$.post("test.php", {
d_id: 1,
date: val
}, function() { /*...*/ },
"json");
You should also check your test.php and ensure that you are sending the Content-Type: application/json
http header (see: http://php.net/manual/en/function.header.php ). 您还应检查test.php并确保发送
Content-Type: application/json
http标头(请参阅: http : //php.net/manual/en/function.header.php )。
Option B: (not recommended) Just remove the JSON.stringify()
part from the callback function: 选项B :(不推荐)只需从回调函数中删除
JSON.stringify()
部分:
function(hstatus){
localStorage.setItem("testJSON", hstatus);
//Retrieving data:
text = localStorage.getItem("testJSON");
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.status;
});
Just a tip: 只是一个提示:
Use the console.log()
command for debug variable values so you can see the exact, typed value in your browser console (press F12 in most browser). 使用
console.log()
命令调试变量值,以便在浏览器控制台中查看确切的类型值(在大多数浏览器中按F12键)。 The alert()
function automatically converts everything into strings so it usually show misleading results. alert()
函数自动将所有内容转换为字符串,因此通常会显示误导性结果。
What I suspect is the format of string which is coming from server. 我怀疑是来自服务器的字符串格式。
if your alert is giving this output: data: {"status": "Active","broadcastmsg": "msg"} Then JSON.parse won't be able to parse it. 如果您的警报提供此输出:data:{“status”:“Active”,“broadcastmsg”:“msg”}然后JSON.parse将无法解析它。 Since it's not an object or any type.
因为它不是一个对象或任何类型。 This will give a syntax error.
这将产生语法错误。
Following is the right format of string which you should send from server then everything JSON.parse will work fine. 以下是您应该从服务器发送的正确格式的字符串,然后JSON.parse可以正常工作。 {"data": {"status": "Active","broadcastmsg": "msg"}}
{“data”:{“status”:“Active”,“broadcastmsg”:“msg”}}
So solution for this which I can guess is: hstatus parameter of success callback is of plainObject So try following: 所以我可以猜到的解决方案是:成功回调的hstatus参数是plainObject所以请尝试以下方法:
myJSON = JSON.stringify(hstatus.data);
Let me know in case it doesnt work, will dig more. 让我知道,如果它不起作用,将挖掘更多。
你可以试试这个:
document.getElementById("demo").innerHTML = obj.data.status;
您需要通过obj.data.status
而不是obj.status
访问您的status
属性
document.getElementById("demo").innerHTML = obj.data.status;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.