2010-02-02 47 views

回答

0

使用XMLHttpRequest对象(或它的基于COM的弟弟):

function getJSON() 
{ 
    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); 
    xhr.open("GET", "http://mysite.com/myscript.asp", true); 
    xhr.onreadystatechange = function() 
    { 
     if (xhr.status == 200 && xhr.readystate == 4) 
     { 
      if (JSON) // provided by json2.js or browsers with native JSON 
       var result = JSON.parse(xhr.responseText); 
      else 
       var result = eval ('(' + xhr.responseText + ')'); 

      // Do something with the result here 
     } 
    } 
    xhr.send(); 
} 

json2.js是更安全的JSON解析一个脚本,可从http://www.json.org/js.html