【问题标题】:how to decode the json ajax response如何解码 json ajax 响应
【发布时间】:2013-04-10 11:36:27
【问题描述】:

我在解码 ajax 响应时遇到问题,在这里我将组织、位置和建筑物作为输入发送,作为回报提供 2 个键作为入口/出口和键,现在我的 ajax 调用工作正常,我可以提醒响应,现在我的要求是解码我得到的 json 数组,然后将入口/出口的值放入表单中名为入口/出口的表单字段中,然后输入表单中的类型字段。我试图解码php phage 中的 json ,它在调用 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>

我没有得到键 entance 或 exit 的值并输入 json iam 得到响应并收到警报是

[{"name":"Default Entrance + Exit","type":"both"}]

我不知道我是否在代码中犯了一些错误,因为我只是从 javascript 开始 谢谢

【问题讨论】:

  • @ArunKillu 将答案发布为答案......:)
  • 我正在收到响应 [object,object]。我想打印值 Default Entrance + Exit 和两者
  • 请确保您的 ajax.php 正确返回。
  • 我相信如果你这样做alert(objJSON[0].name);你的警报会起作用你应该能够从那里弄清楚。

标签: php javascript ajax


【解决方案1】:

出于安全和工作流程的原因,最好通过 JSON.parse

解析 json
var objJSON = JSON.parse(strJSON);

不是由 eval

【讨论】:

  • 谢谢.. 工作就像一个魅力.. 现在我怎样才能将值带入我的表单字段以用于进入/退出和表单中的类型字段
  • 据我了解,您没有使用 jQuery 或任何其他 JS 库...需要支持哪些浏览器?哪个字段需要填写数据?
  • Mozilla 、 chrome 和 Safari 是我想要支持的浏览器。在表单中,在 ajax 调用下方。有一个名为 Entrance/Exit 的字段应填写默认入口/出口,还有一个名为 type 的字段,假设填写 value=both。谢谢楼主
  • document.getElementById('ent').value = objJSON.name; document.getElementById('type').value = objJSON.type;我> | // 您应该将 id="type" 添加到您的类型输入标签中,以便通过此方法获取元素,但它几乎适用于所有浏览器。这些行应该在ajax响应上执行,f.e.而不是您的警报
【解决方案2】:

试试这个,

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

【讨论】:

    【解决方案3】:

    请试试这个

     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

    【讨论】:

      猜你喜欢
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 2019-07-14
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      相关资源
      最近更新 更多