传递JSON与JS xmlhtt prequest请求JS、JSON、prequest、xmlhtt

2023-09-11 01:13:54 作者:゛空气中的烟圈

我呼应的JSON从PHP服务器返回给浏览器。我安静的精通XML不过是新来的JSON。可有人告诉我如何正确地从xmlhtt prequest提取JSON然后将它传递到数据又名警报。

我的JSON(从PHP服务器)

  {数据:{
       消息:打开分离舱门,哈尔,
       类型:请求,
       回复=>阵列(
                 ID:12321
                 消息:对不起戴夫,恐怕我不能这样做!
        )
        }
        }
 

我在JS请求返回的JSON,但是我没有办法捕捉它,或者提取内幕信息的......我的AJAX功能...

 函数AJAX(网站){
    xmlhttp.open(GET,现场,真正的);
    xmlhttp.onreadystatechange =功能(){
    如果(xmlhttp.readyState == 4){
       如果(xmlhttp.status!= 404){
       VAR RESP =新功能(返回+ xmlhttp.responseText)();

     }
   }
 xmlhttp.send(空);
 }
 

那么我在调用window.onload的功能

 的window.onload = runJSON()

功能runJSON(){
    VAR网站=HTTP://localhost/sites/sandbox/json.php
    阿贾克斯(网站);
    ...这就是我不确定......我怎么访问数据对象
}
警报(数据);
}
 
JSON和JS数据类型转化

解决方案

 回复=>阵列(
             ID:12321
             消息:对不起戴夫,恐怕我不能这样做!
    )
 

必须是

 回复:{
  ID:12321,
  消息:对不起戴夫,恐怕我不能这样做!
}
 

在JavaScript中,你可以通过运行 JSON.parse(STR),你应该让你的 STR 从回调:

 如果(xmlhttp.status!= 404){
  VAR RESP =新功能(返回+ xmlhttp.responseText)();
  ajaxDone(RESP);
}
功能ajaxDone(STR){
  尝试 {
    VAR数据= JSON.parse(STR);
    的console.log(数据);
  }赶上(X){
    // TODO:看看发生了什么
  }
}
 

注: JSON.parse 在旧的浏览器不存在。如果你想支持他们太多,你需要一个库,将垫片他们,像jQuery(这会顺带也带走了很多痛苦的AJAX部门)。

I am echoing JSON from a php server back to the browser. I am quiet proficient with XML however am new to JSON. Can someone show me how to correctly extract the JSON from the xmlhttpRequest and then pass it into data aka an alert.

My JSON (from PHP server)

       {"data": {
       "message": "Open the Pod bay doors, Hal",
       "type": "request",           
       "replies" => array(
                 "id": "12321"
                 "message": "I'm sorry Dave, I'm afraid I can't do that!"
        )
        }
        }

My request in JS returns the JSON however I have no way of catching it or extracting the inside information... my ajax function is...

function ajax(site){  
    xmlhttp.open("GET","site",true);
    xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
       if (xmlhttp.status!=404) {
       var resp =new Function("return "+xmlhttp.responseText)();

     }
   }
 xmlhttp.send(null);
 }

Then I am invoking the function in window.onload

    window.onload = runJSON()

function runJSON(){
    var site = "http://localhost/sites/sandbox/json.php"
    ajax(site);
    ... this is what i am unsure about... how do I access the data in the object
}
alert(data);
}

解决方案

"replies" => array(
             "id": "12321"
             "message": "I'm sorry Dave, I'm afraid I can't do that!"
    )

needs to be

"replies": {
  "id": "12321",
  "message": "I'm sorry Dave, I'm afraid I can't do that!"
}

in JavaScript, you can convert JSON to a JS object by running JSON.parse(str), where you should get your str from a callback from inside the onreadystatechange handler:

if (xmlhttp.status!=404) {
  var resp =new Function("return "+xmlhttp.responseText)();
  ajaxDone(resp);
}
function ajaxDone(str) {
  try {
    var data = JSON.parse(str);
    console.log(data);
  } catch (x) {
    // TODO: see what happened
  }
}

Note: JSON.parse does not exist in older browsers. If you wish to support them too, you will need a library that will shim them in, like jQuery (which will incidentally also take away a lot of pain in AJAX department).