开发者

Ajax gets nothing back from the php

Jquery i dont have alert and firefox i dont have anything in return. The code was working before, database query have successfull records also. What i am missing???

  1. Jquery ajax.

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

       case 'teksten_display':
           $id = $_REQUEST['ids'];           
           $res =  $_dclass->_query_sql(
               "select a,b,id,wat,c,d from tb1 where id='" . $id . "'" );
           $_rows = array();

           while ( $rows = mysql_fetch_array ($res) ) { $_rows = $rows; }
   //header('Cache-Control: no-cache, must-revalidate');
   //header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
   header('Content-type: application/json');

           echo utf8_encode( json_encode($_rows) ) ;
           //echo json_encode($_rows);
           //var_dump($_rows);
           //print_r ($res);

           break;
  1. Firefox response/request header

Date Sat, 24 Apr 2010 22:34:55 GMT
Server Apache/2.2.3 (CentOS)
X-Powered-By PHP/5.1.6
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma no-cache
Content-Length 0
Connection close
Content-Type application/json

Host www.xxxx.be
User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100330 Fedora/3.5.9-2.fc12 Firefox/3.5.9
Accept application/json, text/javascript, */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-开发者_Go百科Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://www.xxxx.be/xxxxx
Content-Length 17
Cookie csdb=2; codb=5; csdbb=1; codca=1.4; csdca=3; PHPSESSID=benunvkpecqh3pmd8oep5b55t7; CAKEPHP=3t7hrlc89emvg1hfsc45gs2bl2


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);
    }
}


$.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!


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜