Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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
使用jquery ajax从php获取JSON值_Php_Jquery_Ajax_Json - Fatal编程技术网

使用jquery ajax从php获取JSON值

使用jquery ajax从php获取JSON值,php,jquery,ajax,json,Php,Jquery,Ajax,Json,嗨,朋友们,有人能告诉我这个plz吗。我是这一章的新手。我试图从PHP获取JSON格式的值,但在我运行的时候,我得到了这个输出“SyntaxError:JSON.parse:unexpected charactere”。。我想我在php转换中犯了错误…请帮助我的朋友 我的.html文件 <html> <head> <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script&g

嗨,朋友们,有人能告诉我这个plz吗。我是这一章的新手。我试图从PHP获取JSON格式的值,但在我运行的时候,我得到了这个输出“SyntaxError:JSON.parse:unexpected charactere”。。我想我在php转换中犯了错误…请帮助我的朋友

我的.html文件

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <title>Display Page</title>
</head>
<body>
    <button type='button' id='getdata'>GetData.</button>
    <button type='button' id='get'>sample</button>
    <script type='text/javascript'>
    $('#get').click(function() {
        alert("laksjdflk");
    });
    $(document).ready(function() {
        $('#getdata').click(function() {
            $.ajax({
                url:'neww.php' ,
                dataType:'json' ,
                success:function(output_string) {
                    alert(output_string);
                },
                error:function(xhr,ajaxOptions,thrownError){
                    alert(xhr.statusText);
                    alert(thrownError);
                }
            });
        });
    });
    </script>
</body>
</html>

显示页
获取数据。
样品
$('#get')。单击(函数(){
警报(“laksjdflk”);
});
$(文档).ready(函数(){
$('#getdata')。单击(函数(){
$.ajax({
url:'neww.php',
数据类型:'json',
成功:函数(输出字符串){
警报(输出字符串);
},
错误:函数(xhr、ajaxOptions、thrownError){
警报(xhr.statusText);
警报(thrownError);
}
});
});
});
generatephp.php

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("androidlogin");

    $sql=mysql_query("SELECT* FROM trysave");

    $temp="";
    $i=0;
    while($row=mysql_fetch_assoc($sql)){
        $temp=$row['stringss'];
        $temp.=$row['integerr'];
        $array[i]=$temp;
        i++;
    }
    echo json_encode($array);// this will print the output in json
?>

这可能是因为
未定义数组变量通知
如果未找到
记录,您必须
定义数组

此外,在变量i
之前,您还遗漏了一个给出错误的
$
i
应该是
$i
类似

$array=array();// define here to prevent from "Notice"
while($row=mysql_fetch_assoc($sql))
{
    $temp=$row['stringss'];
    $temp.=$row['integerr'];
    $array[$i]=$temp;// use $ before i
    $i++;// use $ before i
}
echo json_encode($array);// this will print the output in json

还有一件事您提到了PHP文件名作为
generatephp.PHP
,您正在
url:'neww.PHP',
中使用
$.ajax()
,您必须检查代码一次。

这可能是因为
未定义数组变量通知
如果未找到
记录,您必须
定义数组

此外,在变量i
之前,您还遗漏了一个给出错误的
$
i
应该是
$i
类似

$array=array();// define here to prevent from "Notice"
while($row=mysql_fetch_assoc($sql))
{
    $temp=$row['stringss'];
    $temp.=$row['integerr'];
    $array[$i]=$temp;// use $ before i
    $i++;// use $ before i
}
echo json_encode($array);// this will print the output in json
还有一件事你提到了PHP文件名作为
generatephp.PHP
,你在
$.ajax()
中使用
url:'neww.PHP',
,你必须检查你的代码一次。

除了明显的问题(MySQL.*)之外,你的PHP文件应该在响应头中指定输出将是JSON类型。输出默认为
text/html
,Javascript无法将其解析为有效的JSON对象

你可以这样做

<?php
header('Content-type: application/json');
// Rest of the code remains intact
除了明显的问题之外,PHP文件应该在响应头中指定输出的类型为JSON。输出默认为
text/html
,Javascript无法将其解析为有效的JSON对象

你可以这样做

<?php
header('Content-type: application/json');
// Rest of the code remains intact

我会用不同的东西

php:


//您不需要$i变量,因为您将获得java str.length=that$i

 <script type='text/javascript'>
    $('#get').click(function() {
        alert("laksjdflk");
    });

    $(document).ready(function() {
        $('#getdata').click(
        function() {
jQuery.getJSON( "neww.php") //no get vars

.done(function(a) {
//console.clear();
console.log(a);
show_data(a);
})

.fail(function(a) {
console.log( "error" );
});

});
});


function show_data(a){

for(var i=0; i<a.length; i++)
var stringss = a[i].stringss;
var integerr = a[i].integerr;
var nuber_temp = i;
//do stuff with them ...

}
    </script>

$('#get')。单击(函数(){
警报(“laksjdflk”);
});
$(文档).ready(函数(){
$(“#获取数据”)。单击(
函数(){
jQuery.getJSON(“neww.php”)//没有get变量
.完成(功能(a){
//console.clear();
控制台日志(a);
显示_数据(a);
})
.失败(功能(a){
控制台日志(“错误”);
});
});
});
功能显示数据(a){

对于(var i=0;i

我将使用不同的

php:


//您不需要$i变量,因为您将获得java str.length=that$i

 <script type='text/javascript'>
    $('#get').click(function() {
        alert("laksjdflk");
    });

    $(document).ready(function() {
        $('#getdata').click(
        function() {
jQuery.getJSON( "neww.php") //no get vars

.done(function(a) {
//console.clear();
console.log(a);
show_data(a);
})

.fail(function(a) {
console.log( "error" );
});

});
});


function show_data(a){

for(var i=0; i<a.length; i++)
var stringss = a[i].stringss;
var integerr = a[i].integerr;
var nuber_temp = i;
//do stuff with them ...

}
    </script>

$('#get')。单击(函数(){
警报(“laksjdflk”);
});
$(文档).ready(函数(){
$(“#获取数据”)。单击(
函数(){
jQuery.getJSON(“neww.php”)//没有get变量
.完成(功能(a){
//console.clear();
控制台日志(a);
显示_数据(a);
})
.失败(功能(a){
控制台日志(“错误”);
});
});
});
功能显示数据(a){

对于(var i=0;i

请参见以下链接[jquery ajax从php获取json值][1][1]:请参见以下链接[jquery ajax从php获取json值][1][1]: