views:

90

answers:

3
A: 

add this line to you ajax, see if there is any error

success : function(msg){
       alert(msg);
},
error : function(request, status, error) {
    if(status == 'parsererror' || status == 'error') {
        alert(error);
    }
}
Puaka
I get this as alert: undefined
Stackfan
Alert: parseerror. }, error : function(request, status, error) { //if(status == 'parsererror' || status == 'error') { alert(status); //} //if(status=='par) }
Stackfan
I change the database result to simple values, but still i get parseerror?<pre><code>case 'teksten_display': $id = $_REQUEST['ids']; $res = $_dclass->_query_sql( "select '1','1','1','1','1','1' from teksten where id='" . $id . "'" ); $_rows = array(); while ( $rows = mysql_fetch_array ($res) ) { $_rows = $rows; } echo utf8_encode( json_encode($_rows) ) ; break;</code></pre>
Stackfan
so you did get the error alert, then its something to do with your php. is the $_rows return something ? if you have firebug installed, trace the response in console panel
Puaka
notice this select '1','1','1','1','1','1' from teksten where id='" . $id . "'" , doesnt seems to be a valid sql query to me
Puaka
Mysql prompt: SELECT '1', '1', '1', '1', '1', '1'FROM tekstenWHERE id = '1'LIMIT 0 , 30 , i get output : 1 1 1 1 1 11 1 1 1 1 1
Stackfan
parseerror, for number 1, should not i think. Is it related to apache? iso-8859-1 or utf8 or text/html or page returning not really as json content type?
Stackfan
no, it does return json(at least as my last test), im not sure about your server setting, but you can try this , comment the header and you echo then use this exit(json_encode($_rows)); if you dont have firebug, use Developer feature in Chrome, and in you javascript do this, "console.log(typeof(msg))", see if it is a string or object(json)
Puaka
I am on fedora 12/My own server is Centos 5.4. I use firefox, and firebug default. ## console.log( ... ) tried get nothing, ## exit(json_encode(...) ) tried , geting nothing.
Stackfan
last link: http://dpaste.de/vwEz/
Stackfan
hey, i paste something on your snippet. try use normal query(not using your db class), and do a simple query like "select now();"
Puaka
+1  A: 

$.ajax success callback returns whatever you want it to return, you are returning a json OBJECT in this case and it appears as though you are expecting to just display a success message. Your msg variable actually contains an object, not a "Success!" string - in order to display something you will need to use msg['variable_from_json_object'] which will show that value.

Somethings I would look at as well in case its not your jquery that is causing problems in your call to utf8_encode, does this method work to convert an entire json object, or does it need to be run on each item BEFORE its converted to json? json remember is an object, its not a string.

You may look at this to get a better idea of how you can convert your array to uft8, then to json.

echo json_encode(utf8_encode_array($_rows));

with the method supplied in the link possibly..

Lastly, in order to make sure your json is being created successfully, visit the url you are call as ajax, as a normal page: include/add_edit_del.php?model=teksten_display&oper=search&ids=" + _id. of course replacing _id with a value just for testing. I think that you may see an issue here as your url should not include query parameters, they should all be in your data if you are going to use that parameter.. I would expect something of either of the following:

  $.ajax({
  type    : "POST",
  url     : "include/add_edit_del.php",
  data    : "model=teksten_display&oper=search&ids=" + _id,
  dataType: "json",
  success : function(msg){
   alert(msg);
  }
 });

OR not using the data parameter at all and cramming it into the url, because url expect NO query parameters (?var=foo), when it sees data it replaces the url parameters with values supplied within data

  $.ajax({
  type    : "POST",
  url     : "include/add_edit_del.php?model=teksten_display&oper=search&ids=" + _id",
  dataType: "json",
  success : function(msg){
   alert(msg);
  }
 });

Good luck!

Rabbott
Stackfan
The whole thing, that i posted, is a working script. I did a server migration, after that its not working. Now i am confused where is wrong, "THE CODE WAS WORKING".....
Stackfan
last link: http://dpaste.de/vwEz/
Stackfan
The same code that works in my local box but doesnt work in server is here: http://dpaste.de/Hxr5/
Stackfan
If this already worked, and now doesnt, and no code was changed, this is a server configuration issue not a code issue. Posting code isnt going to help anyone solve this problem. You need to post information pertaining to the differences in server/local setup, OS, PHP Versions, MySQL versions, MySQL database types and so on.. lets go from there..
Rabbott
A: 

Anyway, i got it, its CentOS 5.4, in my box: [root@www include]# php -version PHP 5.1.6 (cli) (built: Jan 13 2010 17:09:42) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies [root@www include]#

Solution: http://gargiullo.com/tag/json_encode/

Stackfan
See my comment to my answer..
Rabbott
great :) so its your server
Puaka