javascript 如何解码json ajax响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15924744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to decode the json ajax response
提问by anu
i am having trouble with decoding the ajax reponse ,here i am sending the organisation,location and building as the inputs which in return gives 2 keys as entrance/exit and key ,now my ajax call is working fine and i can alert the response ,now my reqiurement is to decode the json array i have got and then take the value of entrance/exit into a form field called entrance/exit in the form and type into type field in the form .I have tried to decode the json in the php phage which is executed when the ajax is called and stored the 2 values into sessions but it was not showing up in the form fields when i give the value =$_SESSIN[type] and $_SESSION[entrance/exit] after that i have tried to decode the json script with javascript using console can any one figure out what i a doing wrong. the code upto this is
我在解码 ajax 响应时遇到问题,在这里我将组织、位置和建筑物作为输入发送,作为回报,它提供 2 个键作为入口/出口和键,现在我的 ajax 调用工作正常,我可以提醒响应,现在我的要求是解码我得到的 json 数组,然后将入口/出口的值放入表单中名为入口/出口的表单字段中,然后输入表单中的类型字段。我试图解码php phage 在调用 ajax 时执行并将 2 个值存储到会话中,但是当我给出值 =$_SESSIN[type] 和 $_SESSION[entrance/exit] 之后,它没有显示在表单字段中尝试使用控制台用 javascript 解码 json 脚本,任何人都可以弄清楚我做错了什么。到此为止的代码是
//ajax
function ajax()
{
var org=document.getElementById('category_id').value;
alert(org);
var loc=document.getElementById('category_id1').value;
alert(loc);
var bui=document.getElementById('category_id2').value;
alert(bui);
var req;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
req=new XMLHttpRequest();
}
else
{// code for IE6, IE5
req=new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("POST", "ajax.php?&org="+org+"&loc="+loc+"&bui="+bui+"", true);
req.send();
req.onreadystatechange=function(){
if(req.readyState==4&&req.status==200){
//$(".error").hide();
result=req.responseText
alert(result);
var strJSON = 'result';
var objJSON = eval("(function(){return " + strJSON + ";})()");
alert(objJSON.name);
alert(objJSON.type);
}
}
}
<form name="theForm" method="post" action="addmachine.php" enctype="multipart/form-data" onSubmit="return validate();">
<label for="orgname">Organisation Name</label>
<select style="width: 305px;text-align:left ;" name="category_id" id="category_id" onchange="OrganisationName(this);">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<p>
<label name="location">Location</label>
<select style="width: 305px;" name="category_id1" id="category_id1" onchange="LocationName(this);" >
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</p>
<p>
<label for="building">Building</label>
<select style="width: 305px" name="category_id2" id="category_id2" onchange="BuildingName(this);" onchange="ajax(this);">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</p>
<label for="entr/exi">Entrance/Exit</label>
<input type="text" name="ent" id="ent" value="objJSON.name" placeholder="enter entrance/exit"/>
<p>
<label for="type">Type</label>
<input type="text" name="type" value="objJSON.type" placeholder="enter your work station"/>
<label for="name">Name</label>
<input type="text" id="workstnname" name="workstnname" placeholder="enter your work station" onblur="return name();" onkeypress="return onKeyPressBlockNumbers(event);">
<label for="description">Description</label>
<textarea name="description" style="height:150px;width:300px;"></textarea>
<label for="machinetype">Machine Type</label>
<select style="width: 305px;text-align:left;" name="machinetype">
<option value="">Select</option>
<option value="kiosk">kiosk</option>
<option value="workstation">workstation</option>
</select>
<p>
<input type="submit" name="submit" value="Submit">
</p>
</form>
</div>
i am not getting the value of the keys entance or exit and type the json iam getting in response and is alerted is
我没有得到进入或退出键的值,然后输入 json 我得到的响应并收到警报
[{"name":"Default Entrance + Exit","type":"both"}]
i dont know whether i have did some bluders in the code or not,as i have only started with javascript thank you
我不知道我是否在代码中做了一些bluders,因为我只开始使用javascript,谢谢
回答by Valeriy Selitskiy
For the security and workflow reasons it's better to parse json by JSON.parse
出于安全和工作流程的原因,最好通过JSON.parse解析 json
var objJSON = JSON.parse(strJSON);
not by eval
不是通过评估
回答by Shijin TR
Try this,
试试这个,
var objJSON = JSON.parse(result);
alert(objJSON.name);
alert(objJSON.type);