Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at 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..."));

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)