在PHP和jQuery GET之间遇到麻烦

时间:2014-10-07 16:33:50

标签: php jquery html json content-type

我正在尝试将两个变量从HTML页面发送到PHP脚本,但响应仍然以text / html的形式返回。也就是说,PHP文件中的整个代码都将返回到控制台。

我的jQuery代码:

    $.get(                             //call the server
            "biography_query.php",                //At this url
            {
                field: "value",
                id: decodeURIComponent(id),
                name: decodeURIComponent(name)
            }                          //And send this data to it
        ).done(                         //And when it's done
            function(data)
            {    
                console.log(data);
            },"jsonp"
        );

PHP代码:

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

//start session on server to store the users information
session_start();

// establish connection to SQL database
$con = mysqli_connect("localhost","root","","capstone") or die("Error: " . mysqli_error($con));

$id = $_REQUEST['id'];
$name = $_REQUEST['name'];

// build statement to query database and return all characters
$SQL = "SELECT real_name, alternate_identities, aliases, nicknames, place_of_birth, first_appearance FROM `character` WHERE id='$id' AND superhero_name='$name'";

// execute the statement
$sqlReturn = mysqli_query($con, $SQL);

$row = array();
while($r = mysqli_fetch_assoc($sqlReturn)) {
    $row['real_name'] = $r['real_name'];
    $row['alternate_identities'] = $r['alternate_identities'];
    $row['aliases'] = $r['aliases'];
    $row['nicknames'] = $r['nicknames'];
    $row['place_of_birth'] = $r['place_of_birth'];
    $row['first_appearance'] = $r['first_appearance'];
}
    echo json_encode($row);

1 个答案:

答案 0 :(得分:1)

  

“我正在使用<?代码”

根据OP的意愿:(关闭问题,以及将来的读者)

如果未启用short open tags,您需要启用它们,或将<?更改为<?php

以下是关于这个主题的一些文章,在Stack:

在PHP.net上: