PHP文件仅发送"parsererror"消息,通过AJAX [英] PHP files sending only a "parsererror" through AJAX

查看:110
本文介绍了PHP文件仅发送"parsererror"消息,通过AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案:在与@skobaljic进行teamviewer会话后,他发现我实际上不是在localhost中打开html,而是使用文件系统(如file://...).我为浪费大家的时间而道歉.

SOLUTION: After a teamviewer session with @skobaljic he figured out that I was not actually opening the html in localhost but using the file system (as in file://...). I apologize for wasting everyone's time like that.

我正在尝试通过Ajax发送一些php数组并打印出来,尽管得到200 OK响应,但是接收到的html中没有实际效果.状态文本显示为"parsererror".

I am trying to send some php arrays through Ajax and print them out and despite getting a 200 OK response, there is no actual effect in the received html. The status text says "parsererror".

文件如下:

<!DOCTYPE html>
<html>
<head>
<title>Page Title Woo!</title>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js'></script>
</head>
<body>

<h1>Heading</h1>
<p>Paragraph.</p>


        <ul></ul>

        <script type='text/javascript'>
        $(document).ready(function(){

            $.getJSON('DbGetter.php', function(data) {


                    console.log(data);
                    $.each(data, function(key, array) {
                            $('ul').append('<li id="' + key + '">' 
                            + array.longitude + ' ' 
                            + array.latitude + '</li>');
                    });
            })
            .fail(function(error) {
                    console.log(error);
            });

        });
        </script>
</body>
</html> 

和php:

<?php
$servername = "localhost";
$username = "testuser";
$password = "password";
$dbname = "Locations";

header('Content-Type: application/json');

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM places";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    //declare associative array
    $array = array();
    $num = 0;

    // output data of each row
    while($row = $result->fetch_assoc()) {

        //store them in an array
        $place = array(
        'id' => $row["id"], 
        'latitude'=> $row["latitude"] , 
        'longitude'=> $row["longitude"], 
        'place_name' => $row["place_name"],
        'country_code'=> $row["country_code"], 
        'postal_code'=> $row["postal_code"]);


        /*
        echo "Coordinates: " . $row["latitude"]. " " . $row["longitude"]. " - Name: " . $row["place_name"]. "   " .  "<br>";
        */

        //building the second associative array
        $array[$num] = $place;
        $num += 1;

    }

        echo json_encode($array);
} else {
    echo json_encode("0 results");
}
$conn->close();
?> 

我尝试通过Firebug来查看值,但是我是盲目的还是没有将其存储在DOM中的任何位置.一般来说,我对Web应用程序还很陌生,所以我不知道如何调试它.

I've tried looking at the value through firebug, but either I'm blind or it's just not stored anywhere in the DOM. I'm pretty new to web application in general so I don't know how to go about debugging it.

我自己运行php,我得到:[{"id":"1","latitude":"57.0502","longitude":"9.9173","place_name":"1000fryd","c‌​ountry_code":"DK","postal_code":"9000"},{"id":"2","latitude":"58.0502","longitude‌​":"10.9173","place_name":"same_place","country_code":"DK","postal_code":"9000"}] 我期望的是两行.

Running the php by itself I get: [{"id":"1","latitude":"57.0502","longitude":"9.9173","place_name":"1000fryd","c‌​ountry_code":"DK","postal_code":"9000"},{"id":"2","latitude":"58.0502","longitude‌​":"10.9173","place_name":"same_place","country_code":"DK","postal_code":"9000"}] Which are the 2 rows I expect.

也没有Firebug标记的XHR请求.

There is also no XHR request marked by Firebug.

推荐答案

您在此处未对有效的JSON进行编码.如果尝试将其与JQuery的$ getJSON结合使用,则会给您"parseerror".请注意第275-281行中的 JQuery Ajax源.

You're not encoding valid JSON here.. This will give you a "parseerror" if you try using JQuery's $getJSON with it. Notice the JQuery Ajax Source from lines 275-281.

echo json_encode("0 results");

您可能可以将其更改为:

You could probably change it to something like:

echo json_encode(["0 results"]);

这篇关于PHP文件仅发送"parsererror"消息,通过AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆