[英]how to decode the json ajax response
我在解碼ajax響應時遇到麻煩,在這里我將組織,位置和建築物作為輸入發送,作為回報,該輸入提供2個鍵作為進入/退出和鍵,現在我的ajax調用工作正常,我可以提醒響應,現在我的需要是對我得到的json數組進行解碼,然后將entry / exit的值放入表單中的名為entry / exit的表單字段中,並在表單中鍵入type字段。我嘗試對json進行解碼當調用ajax並將其存儲到會話中時將執行兩個php噬菌體,但是當我給定值= $ _ SESSIN [type]和$ _SESSION [entrance / exit]之后,它沒有顯示在表單字段中嘗試使用控制台使用javascript解碼json腳本,任何人都可以弄清楚ia做錯了什么。 到目前為止的代碼是
//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"}]
我不知道我是否在代碼中做了一些bluders,因為我只是從javascript開始,謝謝
出於安全和工作流的原因,最好通過JSON解析json.parse
var objJSON = JSON.parse(strJSON);
不等於
請試試這個
JSONObject obj = new JSONObject(result);
List<String> list = new ArrayList<String>();
JSONArray array = obj.getJSONArray("interests");
for(int i = 0 ; i < array.length() ; i++){
list.add(array.getJSONObject(i).getString("interestKey"));
}
使用org.json庫
嘗試這個,
var objJSON = JSON.parse(result);
alert(objJSON.name);
alert(objJSON.type);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.