0

Magento 1 - I keep getting a parse error. I can't figure out where my syntax is wrong.

$this->getResponse()->setHeader('Content-type', 'application/json');
$this->getResponse()->setBody(json_encode({"user": true}));

This is my ajax request:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
                    $(".emailfocusout").focusout(function(){
                        var email = $('.emailfocusout').val();
                        console.log('EMAIL: ', email);
                        $.ajax({
                            url: '/checkout/Email/Email/email/' + email,
                            type: 'POST',
                            contentType: 'application/json',
                            success: function(data) {
                                console.log('SUCCESS: ', data);
                            },
                            error: function(data) {
                                console.log('ERROR: ', data);
                            },
                        });
                    });
                });
            </script>

1 Answer 1

0

It throws an error because your syntax seems wrong, tries with below syntax

$result = ['user'=>true];

$this->getResponse()->setBody(json_encode($result));

Also, I would suggest you to use core Magento function rather then core PHP function,

$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
1
  • When I check the Network tab, it still says: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data I updated the original question with the ajax request. I trigger the error function with the response. Commented Aug 5, 2018 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.