tags:

views:

418

answers:

4

Hi, i am using JQuery in my app developed using cakePHP and Mysql.. In this JQuery,

                $.ajax({
  type: "POST",
  url: "./updateField",
  data: "name="+fieldname,
  success: function(msg){
                    alert( "Data Saved: " + msg);        }//success
          });//ajax

The database operations are going correctly but if i make use of a alert statement it show me a big html file.. Dont know why its coming.Please suggest me.

+4  A: 

It shows a HTML because that's what you get from the server. I suppose this happens because your AJAX call doesn't actually prevent rendering the page instead of returning an XML or Json structure. I don't know what options you have in CakePHP for returning XML or Json as opposed to rendering the page, but the source of the problem is definitely on server side and not client side.

DrJokepu
+1  A: 

Because that's the response from the POST...

Matthew Flaschen
A: 

Try adding more parameters to success function - first parameter may return not actual result but sender or something alike.

Ivan Suhinin
The first parameter /is/ the result of the POST. The result just happens to be a long document from the server. As stated by DrJokepu, it may be advisable to correct this on the server side.
Matthew Flaschen
+2  A: 

I believe by default, Cake does not run Ajax requests through the main layout, so MOST html should not be there. If not, you can force what layout can be used by specifically calling the render method for a controller and setting the layout to 'ajax'. Reference: http://book.cakephp.org/view/57/Controller-Methods#render-428

I also had issues with debug turned on, because all the SQL statements were getting logged to an HTML table at the bottom of AJAX requests, and caused some serious parsing issues when the data type was expected to be AJAX. This is probably a hack, but I did the following to get around it.

In the main AppController in the beforeFilter() I added the following (make sure all your other controller's beforeFilter methods call the parent method):

 if ($this->RequestHandler->isAjax()) {
          $db =& ConnectionManager::getDataSource('default');
          $db->fullDebug = false;
 }

In cake/libs/model/datasources/dbo_source.php at roughly line 2074 in my version, I changed

if (Configure::read() > 1) { 
  $this->showLog(); 
}

to

if (Configure::read() > 1  && $this->fullDebug) { 
  $this->showLog(); 
}
tschaible