Jonathon Posted April 18, 2011 Share Posted April 18, 2011 Hello, I am unsure exactly how potentially problematic the eval function is. But I have heard that before and so I think I wshould err on the side of caution and use parseJSON(). But I wondered what other people thought of this. Also can I check i'm using thios function right in this book the code is: var data = eval('(' + ajax.responseText + ')'); I tried to use parseJSON() like so: var data = ajax.responseText.parseJSON(); But my results don't show up, what am I doing wrong? (They did work using eval) Thanks Link to comment Share on other sites More sharing options...
HartleySan Posted April 19, 2011 Share Posted April 19, 2011 Most likely the data you are returning from the PHP file is not in the right format. I have read a bit about this topic, and honestly, I think the best solution is to simply return a string in the format of a JS object declaration. For example, if we wanted to declare the following object in JS: var oCar1 = { iWheels : 4, iTopSpeed : 120 }; Then we could return a similar string in PHP as follows: $iWheels = 4; $iTopSpeed = 120; echo '{ iWheels : ' . $iWheels . ', iTopSpeed : ' . $iTopSpeed . ' }'; And then back in your Ajax response event, do the following: var oCar1 = oAjax.responseText; The following site talks more about JSON, which you seem to already be familiar with: http://www.json.org/ Anyway, I just as well figure that avoiding the use of eval() and paraseJSON() altogether is best. 1 Link to comment Share on other sites More sharing options...
Jonathon Posted April 19, 2011 Author Share Posted April 19, 2011 Thanks for your reply. I see what you've done there. But the php file search_results_json.php works. It justs doesn't using the parseJSON command. So the way I'm using the parseJSON must be wrong??I think the rest of the file is ok as it worked using eval. Link to comment Share on other sites More sharing options...
HartleySan Posted April 19, 2011 Share Posted April 19, 2011 All right. I cannot really comment on the eval() or parseJSON() functions, as I never use them. Sorry. If something else strikes me, I'll try and help, but otherwise, please post your solution when you find it. 1 Link to comment Share on other sites More sharing options...
Larry Posted April 21, 2011 Share Posted April 21, 2011 Have you included a parseJSON() library? If so, which one? Have you checked the error console for error messages? Link to comment Share on other sites More sharing options...
Jonathon Posted April 22, 2011 Author Share Posted April 22, 2011 Hello, I think XAMPP comes installed with json support, i checked and it says its enabled json version 1.2.1 I couldn't find out what library it is though (maybe there isn't one). I have been looking at my header. Because I note in your search_results_json.php that you don't have the header in (well i couldn't see it). Mine has it in because it's in the book. Except I get this really strange thing happen. If I leave it it and load the page my computer tries to open the file through my text editor??? This doesn't happen with any other files in my localhost. But if i take this header out header("Content-type: application/json"); then I get the below code returned when i use this url http://localhost/ajax/search_results_json.php?last_name=f [{"name":"Fisher, Sam","department":"Marketing","email":"sam@thiscompany.com"},{"name":"Ford, Zoe","department":"Accounting","email":"zoe@thiscompany.com"},{"name":"Fugate, Nat","department":"Redundancy Department","email":"nat@thiscompany.com"}] but my form returns no results using parseJSON() but eval() works. The error log I have checked already adn the only thing it says is that there is no script tags when the header isn't included. Link to comment Share on other sites More sharing options...
Larry Posted April 22, 2011 Share Posted April 22, 2011 Okay, even if XAMPP comes with JSON installed, and I'm not sure what that means in this case, you still need a parseJSON library installed. And your script needs to include the parseJSON JavaScript library and then call the proper method of the proper class. Don't get distracted by the header(). Right now, if you want to use parseJSON(), you need to focus on choosing, installing, and using a proper parseJSON library. Link to comment Share on other sites More sharing options...
Jonathon Posted April 22, 2011 Author Share Posted April 22, 2011 Okay, even if XAMPP comes with JSON installed, and I'm not sure what that means in this case, you still need a parseJSON library installed. And your script needs to include the parseJSON JavaScript library and then call the proper method of the proper class. Don't get distracted by the header(). Right now, if you want to use parseJSON(), you need to focus on choosing, installing, and using a proper parseJSON library. Ahh ok, thanks Larry, i'll get on to this as I would like the option to be able to use parseJSON() as i'm a little weary of eval() because it gets a bit of bad press Thanks Larry as always Jonathon Link to comment Share on other sites More sharing options...
Recommended Posts