简体   繁体   English

如何解码json ajax响应

[英]how to decode the json ajax response

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 ia doing wrong. 我在解码ajax响应时遇到麻烦,在这里我将组织,位置和建筑物作为输入发送,作为回报,该输入提供2个键作为进入/退出和键,现在我的ajax调用工作正常,我可以提醒响应,现在我的需要是对我得到的json数组进行解码,然后将entry / exit的值放入表单中的名为entry / exit的表单字段中,并在表单中键入type字段。我尝试对json进行解码当调用ajax并将其存储到会话中时将执行两个php噬菌体,但是当我给定值= $ _ SESSIN [type]和$ _SESSION [entrance / exit]之后,它没有显示在表单字段中尝试使用控制台使用javascript解码json脚本,任何人都可以弄清楚ia做错了什么。 the code upto this is 到目前为止的代码是

 //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 我没有得到键entance的值或退出并输入json iam作为响应并被警告

[{"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开始,谢谢

For the security and workflow reasons it's better to parse json by JSON.parse 出于安全和工作流的原因,最好通过JSON解析json.parse

var objJSON = JSON.parse(strJSON);

not by eval 等于

Please try this one 请试试这个

 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"));
    }

using org.json library 使用org.json

Try this, 尝试这个,

  var objJSON = JSON.parse(result);
  alert(objJSON.name);
  alert(objJSON.type);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM