意外的令牌<在JSON的位置0(js和php)

时间:2017-09-28 16:33:21

标签: javascript php html css json

我已经解决了这个问题好几天了,希望有人可以帮我解决这个问题。(尝试将数据从js传递给php)。我想要做的是将lostid从HTML页面传递到js页面,然后js页面发布到php页面,从js页面然后从php获取数据。问题发生在我的js代码中的json = JSON.parse(data);

*注意:当我将sql查询更改为删除或选择所有语句时,它可以正常工作,但选择where子句并不是。

function clickedMyLostDetails(lostid){
var lostitemid = lostid;

$$.ajax({
type: 'post',
url: 'http://localhost/API/RetrieveLostItemAPI.php',
dataType: 'JSON',
data: {LostsItemID:lostitemid },
success: function(output) {
              alert(output);
          },
  error: function(request, status, error){
    alert("Error: Could not delete");
  }
})               

  $$.get('http://localhost/API/RetrieveLostItemAPI.php', {}, function (data) {
        json = JSON.parse(data);
            for(var i = 0; i < json.length; ++i) {
              document.getElementById('itemname').value = json[i]["lostid"];

              console.log(json[i]["lostid"]);

          mainView.router.load({
          url: 'LostItemDetails.html',
          ignoreCache: false

        });

        }

    });

 }

我的PHP代码:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "lostfound";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbName);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());

 }


 $lostsid = $_POST['LostsItemID'];


 $sql    = "SELECT * From lostitem WHERE LostItemID = '$lostsid'";
    $res    = mysqli_query($conn, $sql);
    $lostitems = array();
    if ($res->num_rows > 0) {
        // output data of each row
        while($lostitem_row = $res->fetch_assoc()) {
            array_push($lostitems, array(
                    'lostid' => $lostitem_row["LostItemID"],
                    'userid' => $lostitem_row["UserID"],
                    'itemid' => $lostitem_row["ItemID"],
                    'venueA' => $lostitem_row["VenueA"],
                    'venueB' => $lostitem_row["VenueB"],
                    'venueC' => $lostitem_row["VenueC"],
                    'lostdate' => $lostitem_row["LostDate"],
                    'losttimefrom' => $lostitem_row["LostTimeFrom"],
                    'losttimeto' => $lostitem_row["LostTimeTo"],
                    'imageid' => $lostitem_row["Image"],
                    'desc' => $lostitem_row["Description"],
                    'expdate' => $lostitem_row["ExpiryDate"]
                ));
        } 
    } 

    $json = json_encode($lostitems);
    echo ($json);


 mysqli_close($conn);

  ?>

3 个答案:

答案 0 :(得分:0)

删除print_r($json);

应该只有一个输出

答案 1 :(得分:0)

在你的php中你应该指定你返回一个JSON,所以添加这个为echo

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

也在ajax调用中添加:

contentType: "application/json",

答案 2 :(得分:0)

`

$invoice_status = []; //global variable

foreach ($item_past_data as $key => $old_items) 
{
   if($old_items->item_id == $item_id)
   {
       if($old_items->item_name != $item_name)
       {
           $invoice_status = "Item name changed from ".$old_items->item_name." to ".$item_name;
       }
   }
}`