Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP在javascript函数中检索json_Javascript_Php_Jquery_Json - Fatal编程技术网

用PHP在javascript函数中检索json

用PHP在javascript函数中检索json,javascript,php,jquery,json,Javascript,Php,Jquery,Json,我想阅读json值。 我编写代码的方式如下所示。 其中x将是$movieID <script> function bookBTN(x) { <?php $url = 'http://movie.com/movieOne?seatNum=' . x; $jsonURL = file_get_contents($url); $obj = json_decode($jsonURL,true); f

我想阅读json值。 我编写代码的方式如下所示。 其中x将是$movieID

<script>
     function bookBTN(x) {
        <?php $url = 'http://movie.com/movieOne?seatNum=' . x;
         $jsonURL = file_get_contents($url);
         $obj = json_decode($jsonURL,true); 
            foreach($obj as $data)
            {
             $res = $data['avail'];
            }
           if ($res == "yes"){
             echo "alert ('SUCESS');";
           }
           else
           {
             echo "alert ('FAIL');";

           }


        ?>

     }
</script>

不能将JS和PHP用作同一级别的函数。PHP将在任何JS启动之前执行。因此,您的代码将抛出
未定义常量x
,因为JS中的
x
与PHP中的
x
不同,而且PHP中的变量必须以
$x
开头


对于您的函数,您不需要PHP。使用jqueryajax

 function bookBTN(x) {
     $.ajax({
         url: 'http://movie.com/movieOne?seatNum='+x,
         dataType: 'json',
         success: function (obj) {
             var found = false;

             obj.each(function (i, res) {
                 if (res.avail == 'yes') {
                     alert('SUCCESS');
                     found = true;

                     return false;
                 }
             });

             if (!found) {
                 alert('FAIL');
             }
         }
     });
 }

您可能希望将url称为jsonp数据类型,因为它看起来像是从不同的域调用url。 注意,$.ajax是一个jquery函数


$.ajax({
url:“http://movie.com/movieOne?seatNum=“+x,
jsonp:“回调”,
数据类型:“jsonp”,
数据:{query:“foo”},
成功:功能(响应){
//对响应数据执行任何操作
}
});假设PHP可以工作(尽管取决于它如何将
x
视为文字,它可能不会),那么页面上的输出将是
函数bookBTN(x){alert('suces');}
函数bookBTN(x){alert('FAIL');}
。我怀疑那是你的本意。您不能像这样在脚本之间交换
x

PHP在服务器上运行,JavaScript在浏览器上运行,在完全不同的上下文中运行。PHP运行以创建页面内容,其中可能包括一些HTML、CSS、JS等。PHP脚本的输出是一个页面,然后将该页面发送到客户端机器上的浏览器,以进行显示和交互。这意味着您可以使用PHP生成一些静态JS,然后在页面加载时运行这些JS

如果您希望在加载页面后在JS和PHP之间进行一些交互,则需要使用AJAX调用以双方都能理解的方式在客户端和服务器之间发送数据

然而,对于这个特定的简单示例,您可能根本不需要任何PHP,因为假设这个移除服务器支持CORS,您可以使用AJAX直接从JavaScript调用它。请注意,您的PHP代码在foreach中也有一个逻辑错误,因为它只会匹配最后一个“data”值,以查看它是否包含“yes”,因为该测试在循环之外

本例使用jQuery语法执行ajax GET请求并循环数据,但也可以使用vanilla JS执行这两种操作

编辑:现在很清楚,远程服务器不接受CORS请求。因此,我们需要使用PHP服务器作为中介。因此,您的解决方案需要分为两部分。此外,函数返回的数据结构与PHP代码片段最初建议的不同:

1) 您现有的网页,使用相同的代码,但ajax调用更改为指向您自己的PHP服务器:

<script>
     function bookBTN(x) {
        alert("x = " + x); //you can use this for testing, and remove later
        $.getJSON('http://yourservername/movieOne.php?seatNum=' + x, function(data) { 
          if (data.avail == "yes") {
            alert("Success"); //note correct spelling of "success"
          }
          else { alert("Failure"); } //only if we didn't find anything
        });
     }
</script>

功能书BTN(x){
警报(“x=”+x);//您可以将其用于测试,稍后再删除
$.getJSON('http://yourservername/movieOne.php?seatNum='+x,函数(数据){
如果(data.avail==“是”){
警告(“成功”);//注意“成功”的拼写是否正确
}
否则{alert(“Failure”);}//除非我们没有找到任何东西
});
}
2) 服务器上用作中介的PHP脚本。出于示例的考虑,我将其命名为movieOne.php,以反映它在远程服务器上调用movieOne URL

<?php
  $seatNum = $_GET["seatNum"]; //retrieve the seat number from the browser ajax request
  $url = 'http://movie.com/movieOne?seatNum=' . $seatNum;
  $result = file_get_contents($url); //make the request to the remote server
  echo $result; //echo the result directly back to the ajax call without processing it. If this doesn't return JSON already, then try using "json_encode($result);" instead
?>

$url=”http://movie.com/movieOne?seatNum=“+x
+
更改为
为什么需要使用PHP?为什么不使用
$.get()
/
$.post()
?嗨,我必须定义res吗?我不明白carres=false和data.each(函数(index,val)是什么意思
res
是一个布尔值,用于保存我们是否找到了您要搜索的项目。因此,是的,我们必须给它一个初始值false,直到找到某个项目。
。每个
就像做一个foreach循环。我尝试了您编写的代码,但在我这方面它不起作用。警报框没有显示。好的,所以您需要进行一些基本调试。1)浏览器控制台中是否有错误?2) “bookBtn”函数实际运行了吗?(要进行测试,请在函数开始处添加一行
警报(“x=“+x”);
,这样我们也可以检查x的值)。3) 如果函数运行正常,ajax调用是否成功(检查浏览器的“网络”选项卡,查看调用movie.com是否返回“200”状态或其他内容)?另外,我还注意到脚本中有一个小错误,请检查它并使用我的更新版本。我试过了,但按钮似乎仍然不起作用
<?php
  $seatNum = $_GET["seatNum"]; //retrieve the seat number from the browser ajax request
  $url = 'http://movie.com/movieOne?seatNum=' . $seatNum;
  $result = file_get_contents($url); //make the request to the remote server
  echo $result; //echo the result directly back to the ajax call without processing it. If this doesn't return JSON already, then try using "json_encode($result);" instead
?>