2

There is any reason why a json string fail to be evaluated (transport.responseText.evalJSON();) on the server but works on my local installation ?

I'm doing a simple ajax request like this one:

new Ajax.Request(
        this.saveUrl, {
            method: 'post',
            parameters: Form.serialize(this.form),
            onLoading: function () {
                totals.update('<div class="loading-ajax">&nbsp;</div>');
            },
            onSuccess: function (transport) {
                if (transport.status == 200) {
                    var data = transport.responseText.evalJSON();
                    totals.update(data.shipping_method);
                }
            },
            onFailure: checkout.ajaxFailure.bind(checkout)
        }
    );

On server side I output an array containing some html:

 $data = array(
            'shipping_method' => $shipping_method,
            'payment_method' => $payment_method
        );

echo json_encode($data);       

I have tried to valorize $total and '$other' as empty string '' but I get the same result: transport.responseText.evalJSON(); return an "unexpected token"

As said above the weird thing is that on my local it works ( the output is the same as the server but js doesn't trigger any error )

I'm struggling with this almost all day ... any help is really appreciate thanks

UPDATE:

console.log(transport.responseText) 
-> {"shipping_method":"","payment_method":""}

Inspecting the server response 'network tab' in chrome I can see a small difference, in comparison to my local: there is a small red dot before the response content if ( is say \ufeff if I move the mouse over it, I'm not sure about the meaning ... )

enter image description here

WonderLand
  • 5,494
  • 7
  • 57
  • 76
  • What happens when you do JSON.parse(transport.responseText)? And can you show the ouput of console.log(transport.responseText) – Stranded Kid Sep 23 '15 at 11:19
  • Have you tried the "Network" tab on your "Element inspector", available on your favorite browser? – Ismael Miguel Sep 23 '15 at 11:19
  • 1
    You could try using [jQuery.parseJSON](http://api.jquery.com/jquery.parsejson/) instead off evalJSON. Its what I tend to use, not sure what your actual error is though – Crecket Sep 23 '15 at 11:20
  • If the output is the same and the Javascript code is the same - then it works. You are making some wrong assumption. – fdreger Sep 23 '15 at 11:21
  • Have you configured charset in your php response header ? – angellica.araujo Sep 23 '15 at 11:33
  • I have updated the answer – WonderLand Sep 23 '15 at 11:40
  • indeed @angellica.araujo I think you pointed the finger in the right direction: `\ufeff` character looks to be the guilty ( I' m not sure why on my local this doesn't not create issue but on the server yes ) – WonderLand Sep 23 '15 at 12:04
  • 1
    https://en.m.wikipedia.org/wiki/Byte_order_mark so probably one of your php servers has mbstring extension and the other one doesn't. – Phil Sep 23 '15 at 12:19
  • I have checked and both live server and local server has the `mbstring extension` enabled ... anyother idea ? – WonderLand Sep 23 '15 at 12:29

2 Answers2

3

After some test I found out that the issue is encoding used in some PHP files, my co-worked had the brilliant idea to switch to ANSI. (ISO-8859)

For some reason these files coded with ANSI produced JSON content to be different:

  • chrome inspector was showing a red dot -> Byte_order_mark \ufeff character (aka BOM)

This probably means the BOM character is missing and this was breaking the json parse.

SOLUTION:

After I parsed and cleaned all my project files from the BOM character, chrome doesn't show the red dot in the inspector and the issue is gone

Still it is not clear why on my local installation the issue was not present while on the server it was ( both linux ) but there are probably multiple settings involved here ...

To clean your files see: Elegant way to search for UTF-8 files with BOM?

Community
  • 1
  • 1
WonderLand
  • 5,494
  • 7
  • 57
  • 76
0

you can use map function to evaluate json value

       $.map(transport, function (item){    
 totals.update(item.total);;
       });

I think its Working fine

manish
  • 68
  • 1
  • 5
  • I would suggest him to use JSON.parse() instead, take a look at http://stackoverflow.com/questions/7299644/why-does-this-json-parse-return-error-unexpected-token-illegal – angellica.araujo Sep 23 '15 at 11:31