【问题标题】:Fetch json_encode from php in javascript [duplicate]在javascript中从php中获取json_encode [重复]
【发布时间】:2023-03-24 14:17:02
【问题描述】:

尝试将 php 中的 json_encode 变量传递给 javascript。在 javascript 中得到一个未捕获的错误。以前可以用,什么都没改,现在不行了?!?

JSON 变量适用于 PHP。在php中尝试print_r,OK。浏览器网络也可以。

我想在 PHP 中传递的变量:

$response['id'] = $id;
$response['name'] = $name;
$response['note'] = $note;
$response['image'] = $image;

$response['bumbu'] = $bumbu;
$response['banyak'] = $banyak;

$response['masak'] = $masak;

$response['images'] = $images;

print_r($response);
json_encode($response);

我的javascript:

$('#viewResep')
  .on('show.bs.modal', function (e) {
    var id = $(e.relatedTarget)
      .data('id');
    console.log('Requested : ' + id);

    var xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function () {
      if (this.readyState == 4 && this.status == 200) {
        // document.getElementById("demo").innerHTML = this.getAllResponseHeaders();
      }
    };

    xhttp.open('GET', 'process_post.php?view=' + id, true);
    xhttp.send();

    fetch('process_post.php?view=' + id)
      .then(text => JSON.parse(text))
      .then((data) => {

        r_Name = data.name;
        r_Note = data.note;
        r_Image = data.image;
        r_Bumbu = data.bumbu;
        r_Banyak = data.banyak;
        r_Masak = data.masak;
        r_Images = data.images;
      });
  });

我想在我的 javascript 中使用 json_encode 获取所有变量

【问题讨论】:

  • 您打印了print_r 输出,并丢弃了json_encode 结果。删除print_r 并将echo 添加到下一行。
  • 删除print_r(),只删除echo json_encode($response);
  • 您不需要同时使用XMLHttpRequestfetch(),因为它们基本上都在做同样的事情。只需使用fetch(`process_post.php?view=${encodeURIComponent(id)}`).then(res => res.json()).then(data => { ... })
  • 确保您的 PHP 脚本不输出 echoprint 或以其他方式输出 echo json_encode($response); 以外的任何内容。目前,它看起来像是在打印一些 HTML。请查看问题顶部链接的重复项
  • 你是对的菲尔!我不小心把 print_r 放在了上面的某个地方。现在它可以工作了,但不要将数组正确传递给 javascript。呸!

标签: javascript php json fetch


【解决方案1】:

使用 Ajax 而不是这个............

更新->

PHP 页面

$response['id'] = $id;
$response['name'] = $name;
$json = array("id" =>$response['id'],"name" =>$response['name']);

JS

$.ajax({
type: "POST",
url: "myphppage.php",
success: function(result) {
alert(result.id);
alert(result.name);
}
});

【讨论】:

  • $ajax 返回非函数错误
  • $.ajax({ type: "POST", url: "myphppage.php", success: function(result) { alert('success'); } } });
  • 在这里分享您的确切错误..
  • resep.js:8 Uncaught TypeError: $.ajax is not a function at HTMLDivElement. (resep.js:8) at HTMLDivElement.dispatch (jquery-3.3.1.slim.min .js:2) 在 HTMLDivElement.v.handle (jquery-3.3.1.slim.min.js:2) 在 Object.trigger (jquery-3.3.1.slim.min.js:2) 在 HTMLDivElement. (jquery-3.3.1.slim.min.js:2) 在 Function.each (jquery-3.3.1.slim.min.js:2) 在 w.fn.init.each (jquery-3.3.1. slim.min.js:2) at w.fn.init.trigger (jquery-3.3.1.slim.min.js:2) at otshow (modal.js:119) at HTMLDivElement. (modal. js:535)
  • 检查我的更新答案或检查这里...使用 Ajax 而不是这个............ 更新 PHP页面 $response['id'] = $id; $response['name'] = $name; $response['note'] = $note; $response['image'] = $image; $response['bumbu'] = $bumbu; $response['banyak'] = $banyak; $response['masak'] = $masak; $response['images'] = $images; $json = array("id" =>$response['id'],"name" =>$response['name']); JS $.ajax({ type: "POST", url: "myphppage.php", success: function(result) { alert(result.id); alert(result.name); } });
猜你喜欢
  • 2015-05-11
  • 2011-11-05
  • 2013-10-09
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多