2

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;
}
17
  • remove the ' in '{" Commented Jun 15, 2017 at 9:24
  • @guradio : If you don't mind can you point me which line? I could not find '{" text Commented Jun 15, 2017 at 9:26
  • '{"title":"Inside o.... Commented Jun 15, 2017 at 9:27
  • In your sample response Commented Jun 15, 2017 at 9:27
  • @Purus You may have to share the rest of your code... the portion of the code you shared certainly shouldn't emit a single quote. Commented Jun 15, 2017 at 9:28

1 Answer 1

2

A common reason for unexpected leading characters in the response of PHP is that there is a character before the <?php in any of the included/involved php scripts.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.