JSON PHP Javascript - Simple Working Example

HI, I am using the following code **request.js** var request; function runAjax(JSONstring) { // function returns "AJAX" object, depending on web browser // this is not native JS function! request = new XMLHttpRequest(); request.open("GET", "request.php?json="+JSONstring, true); request.onreadystatechange = sendData; alert('called'); request.send(null); } function sendData() { // if request object received response if(request.readyState == 4) { // parser.php response var JSONtext = request.responseText; // convert received string to JavaScript object try { //var JSONobject = eval('(' + JSONtext + ')'); var JSONobject = JSON.parse(JSONtext); } catch(e) { var err="Error: "+e.description; alert(err); } alert('1'); // notice how variables are used try { var msg = "Number of errors: "+JSONobject.errorsNum+ "\n- "+JSONobject.error[0]+ "\n- "+JSONobject.error[1]; alert(msg); } catch(ee) { var errr="Error: "+ee.description; alert(errr); } } } The php function I have used here is **request.php** I'm calling this JavaScript function from a HTML page **request.html** call
The problem here is I was getting **Syntax Error** at the line:24 var JSONobject = JSON.parse(JSONtext); if I was using var JSONobject = eval('(' + JSONtext + ')'); I was getting ***" ) Expected Error "*** *After that I deleted browser cache. Restarted the browser, now the code seems working very fine.*

以上就是JSON PHP Javascript - Simple Working Example的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JSON 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准