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
Che Figueroa
11 gold badge1 silver badge2 bronze badges
1 Answer 1
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
Keyur Shah
18.1k6 gold badges68 silver badges80 bronze badges
-
When I check the Network tab, it still says:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON dataI updated the original question with the ajax request. I trigger the error function with the response.Che Figueroa– Che Figueroa2018年08月05日 20:30:32 +00:00Commented Aug 5, 2018 at 20:30
Explore related questions
See similar questions with these tags.
default