I am getting the below error. I am not sure where I get the ' symbol in the JSON. I have double checked and sure that the json_encode function of PHP does not add the ' symbol in the first position.
I have seen other question and solutions in StackOverflow and could not find any other solution in Google for this particular scenario.
SyntaxError: Unexpected token ' in JSON at position 0
at parse (<anonymous>)
at e.parseJSON (jquery-migrate.min.js:2)
at fn (jquery.min.js:6)
at k (jquery.min.js:6)
at XMLHttpRequest.<anonymous> (jquery.min.js:6)
Below is the PHP code which generates the JSON string.
$info = Embed\Embed::create($videoUrl);
$result = array();
$result['title'] = $info->title;
$result['desciption'] = $info->description;
$result['type'] = $info->type;
$result['tags'] = $info->tags;
$result['provider'] = $info->providerName;
echo json_encode($result,true);
Below is the jQuery used to do a post action.
$(document).ready(function () {
$("#videoUrl").blur(function (event) {
if ($(this).val() != '') {
$.ajax({
type: 'POST',
url: '/url-here',
dataType: 'json',
data: {
"value": $(this).val()
},
success: function (msg) {
alert("Data Saved: " + msg);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(errorThrown.message);
}
})
}
});
});
The sample response as found in the Chrome console is provided for your reference.
'{"title":"Inside of big gold mine in Africa. How it works. Documentary.","desciption":"Deepest mine in Africa.","type":"video","tags":["Megastructures","mine","gold","deepest mine","documentary","Deepest","how it works"],"provider":"YouTube"}
EDIT:
As requested I am sharing the entire php code.
public function getVideoDetails() {
OW::getResponse()->clearHeaders();
OW::getResponse()->setHeader('Content-Type', 'application/json; charset=utf-8');
$videoUrl = $_POST['value'];
$info = Embed\Embed::create($videoUrl);
$result = array();
$result['title'] = $info->title;
$result['description'] = $info->description;
$result['type'] = $info->type;
$result['tags'] = $info->tags;
//$result['image'] = $info->image;
$result['provider'] = $info->providerName;
echo json_encode($result, true);
OW::getResponse() - > sendHeaders();
exit;
}
'in'{"'{"title":"Inside o....