Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at XMLHttpRequest

时间:2021-05-31 23:24:27

标签: javascript php html json xmlhttprequest

我正在尝试发出 xmlhtttprequest 请求,但总是出现此错误,而且我被告知只有在 html tag 中存在某些 php 时才会发生此类错误php 代码,我看不到它在哪里。

当然,如果不是function connection() { document.getElementById("characterSpinnerSection").innerHTML = ""; document.getElementById("comicsSpinnerSection").innerHTML = ""; var xhr = new XMLHttpRequest(); var name = document.getElementById("name").value; var params = "name=" + name; xhr.open("GET", "./connections/name-search.php?" + params , true); xhr.onloadstart = function() { document.getElementById("characterSpinnerSection").innerHTML = '<strong id="spinnerText" class="text-primary">Loading character...</strong>' + '<div class="text-primary spinner-border ml-auto" role="status" ' + 'aria-hidden="true" id="spinner"></div>'; }; xhr.onload = function() { if (this.status == 200) { const object = JSON.parse(this.responseText); const results = JSON.parse(object); console.log(results["data"]); if (results["data"].count === 0) { document.getElementById("characterSection").innerHTML = '<h2 id="characterMainTitle"><span style="font-weight:bold;">No results for... ' + name + "</span>" + ". Try again.</h2>"; document.getElementById("characterSpinnerSection").innerHTML = ""; document.getElementById("comicsSpinnerSection").innerHTML = ""; }}} 文件的问题,请注明出处。

Arquivo Js:

<?php
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) {
  if (isset($_GET['name'])) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $name_to_search = htmlentities(strtolower($_GET['name'])); // HuLk == hulk

    $ts = time();
    $public_key = 'something';
    $private_key = 'another thing';
    $hash = md5($ts . $private_key . $public_key);

    $query = array(
      "name" => $name_to_search, // ""
      "orderBy" => "name",
      "limit" => "20",
      'apikey' => $public_key,
      'ts' => $ts,
      'hash' => $hash,
    );

    $marvel_url = 'https://gateway.marvel.com:443/v1/public/characters?' . http_build_query($query);

    curl_setopt($curl, CURLOPT_URL, $marvel_url);

    $result = json_decode(curl_exec($curl), true);

    curl_close($curl);

    echo json_encode($result);

  } else {
    echo "Error: no name given.";
  }
} else {
  echo "Error: wrong server.";
}
?>

Arquivo PHP:

Bitmap bitmap = movieImage.getDrawingCache();
String path = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bitmap, "Title", null);
            

Intent shareIntent = new Intent();
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setAction(Intent.ACTION_SEND);
//without the below line intent will show error
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My subject line");
shareIntent.putExtra(Intent.EXTRA_TEXT, movie.getName());
Uri uri = Uri.parse(path);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(shareIntent, "Share via..."));

0 个答案:

没有答案