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>
Keyur Shah
18.1k6 gold badges68 silver badges80 bronze badges
asked Aug 5, 2018 at 19:21

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));
answered Aug 5, 2018 at 19:50
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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.