我在解码ajax响应时遇到问题,在这里我发送组织,位置和建筑物作为输入,作为回报给出2个键作为入口/出口和键,现在我的ajax调用工作正常,我可以提醒响应,现在我的要求是解码我得到的json数组,然后将入口/出口的值放入表单字段中,称为入口/出口,并将其输入到表单中的类型字段中。我试图解码php噬菌体中的json,当ajax被调用时执行,并将2个值存储到会话中,但当我给出值= $ _ SESSIN [type]和$时,它不会显示在表单字段中_SESSION [入口/出口]之后,我试图用JavaScript解码使用控制台的JavaScript的JSON脚本可以让任何人知道什么是做错了。 高达这个代码是如何解码json ajax响应
//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>
我没有得到键的值entance或退出,并响应类型 json的IAM越来越并警告是
[{"name":"Default Entrance + Exit","type":"both"}]
我不知道是否我在代码中做了一些模糊或不正确的,因为我只开始使用javascript 谢谢
JSON.parse(data); – 2013-04-10 11:38:05
@ArunKillu发表答案作为回答.... :) – 2013-04-10 11:41:58
Iam得到答复[对象,对象]。我想打印值默认入口+退出和 – anu 2013-04-10 11:42:33