Welcome Guest, Not a member yet? Register   Sign In
Xajax Whitespace error?
#1

[eluser]acoldone[/eluser]
I've been having trouble getting xajax to work with codeigniter. I set up a simple test


Code:
<?php
class TestXajax extends Controller {

    function TestXajax()
    {
        parent::Controller();    
        $this->load->library('Xajax_lib');
        $this->xajax = new xajax();
        $this->xajax->configure('debug', true);
        
        $this->xajax->registerFunction(array("process_form_data", &$this, "process_form_data"));
        
    
  
          $this->xajax->processRequest();
  
    
        
    }
    
    function index()
    {
        $this->load->view('testview','');
    }
    

    
    function process_form_data($someVar)
    {
        $objResponse = new xajaxResponse();
        
        $objResponse->assign("testDiv", "innerHTML", $someVar);
        
        return $objResponse;
        
        
    }
}
?>
view
Code:
<html>
<head>
<title>TESTING</title>
<?=isset($this->xajax)?$this->xajax->printJavascript($this->config->item('base_url').'assets/scripts'):""; ?>
</head>
<body>
<div id="testDiv"></div>
&lt;INPUT TYPE="button"  value="Test" onclick="xajax_process_form_data('this is a test');return false;"&gt;
&lt;/body&gt;
&lt;/html&gt;


I'm not sure if this is an encoding problem or what. I am testing on a windows machine with apache. I looked for whitespace in the files but couldn't find any. Here is the first part of my xajax debug output:

Code:
Mon Sep 24 2007 17:52:56 GMT-0700 (Pacific Daylight Time)

ERROR: ResponseReceived: Invalid response XML: The response contains an unexpected tag or text: {data}.

Mon Sep 24 2007 17:52:56 GMT-0700 (Pacific Daylight Time)

RECEIVED [status: 200, size: 112 bytes, time: 110ms]:
[1 space here]&lt;?xml version="1.0" encoding="utf-8" ?&gt;
<xjx>
<cmd n="as" t="testDiv" p="innerHTML">this is a test</cmd>
</xjx>

Here is the javascript error generated in FF:

Code:
Error: xml declaration not at start of external entity
Source File: http://www.wctesting.com/_admin/index.php/testXajax
Line: 1, Column: 2
Source Code:
&lt;?xml version="1.0" encoding="utf-8" ?&gt;<xjx><cmd n="as" t="testDiv" p="innerHTML">this is a test</cmd></xjx>-^

There is one space before the &lt;?xml version="1.0" encoding="utf-8" ?&gt; in the xajax debug output (doesn't show up here). Should apache be set to a utf-8 character set? Do all my files have to set to utf-8? I tried changing apache to utf-8 and saving these two files in the same format and it didn't seem to make a difference. Is there any way of stripping out the spaces using a setting in xajax or php itself? Thanks for any help. This has been driving me bonkers.
#2

[eluser]acoldone[/eluser]
I forgot to mention I am using xajax_0.5_beta_4b.
#3

[eluser]acoldone[/eluser]
As soon as I post it I discover the error. My god the time I wasted. It was a whitespace error in the library file:

Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Xajax_lib {
function Xajax_lib() {
    require_once(BASEPATH.'/libraries/xajax_core/xajax.inc'.EXT);
}
}
?&gt;

There was a space on the end. DOH!!!!!
#4

[eluser]alpar[/eluser]
to avoid such problems it's recommended to avoid PHP's closing tag ?&gt; this is perfectly accepted by php, and this way no final whitespace or newline will be outputed
#5

[eluser]Michael Wales[/eluser]
Quote:to avoid such problems it’s recommended to avoid PHP’s closing tag ?&gt; this is perfectly accepted by php, and this way no final whitespace or newline will be outputed

Really? I mean, I know PHP will notice the EOF and find the last line with content, then append '\n?&gt;' to that line - but has anyone really recommended not closing the tag? You'd think the CPU cycles spent in processing this dynamic closing (especially across the lifetime of the site) would far outweigh the few minutes it takes to find your whitespace bug.
#6

[eluser]alpar[/eluser]
This is a coding standard in the Zend Framework check here I never considered performance to be honest, but i don't think is that difficult for php you reach the eof then processing is done regardless if the tag is closed or not, whitespace and newlines aren't considered anyway between the tags, so it has to be some kind of optimization for that.
#7

[eluser]Michael Wales[/eluser]
404 on that link.

I'm not doubting your or anything - just something I've never heard of. Interesting, nonetheless.
#8

[eluser]alpar[/eluser]
http://framework.zend.com/wiki/display/Z...Formatting

this is the link... i copy/pasted it before but maybe i did something wrong or there is something with the site. Anyway a simple search with "your favorite Google Smile" on "Zend framework coding standards" will take you to a ZF wiki page where you can find this information
#9

[eluser]ELRafael[/eluser]
Quote:B.2.1. General

For files that contain only PHP code, the closing tag ("?&gt;") is never permitted. It is not required by PHP. Not including it prevents trailing whitespace from being accidentally injected into the output.

IMPORTANT: Inclusion of arbitrary binary data as permitted by __HALT_COMPILER() is prohibited from any Zend framework PHP file or files derived from them. Use of this feature is only permitted for special installation scripts.

But remmember, this is for Zend Framework, right? I never use like this... A non-end file? Like that movie....

The link : http://framework.zend.com/manual/en/codi...ng.general
#10

[eluser]jz[/eluser]
Hello All,

I'm a first-time CI user, and have been using Xajax for more than a year. Just wanted to add this CI-specific "fix" to a dreaded Xajax foible..

After hours of suffering from Xajax's 0.5 Beta 4's cryptic message:
Quote:ERROR: ResponseReceived: Invalid response XML: The response contains an unexpected tag or text: {data}.

... I finally figured out that my CodeIgniter MODEL FILE was the culprit. It is loaded and used in one of my Xajax-registered methods in my controller. By using the word "culprit" I'm not suggesting this is CI's fault, btw. It's astonishingly lame that Xajax is vulnerable to this kind of idiotic bug...

Moral of my story:
"When using CodeIgniter+Xajax, check your model files for trailing spaces or lines that are used to generate ajax responses."

Hope this helps someone else,

-moo




Theme © iAndrew 2016 - Forum software by © MyBB