为什么AJAX代码在PHP中不起作用?

JS 和 HTML:

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function load_bkissue(accessid){
   //alert(accessid);   
        $.ajax({
    type: "POST",
    url: "2.php",
    async: false,
    dataType: "json",
    data: "accession_id=" + accessid,
    success: function (response) {
        $("#bkid").val(response.accession_id);
        $("#txtbnam").val(response.title);
        $("#txauth").val(response.author);
        $("#txtcat").val(response.category);
        $("#txtsub").val(response.subject_type);
        //$("#txt_div").val(response.rack_no);
        $("#txtrack").val(response.rack_no);
    }
});
        </script>
</head>
<body>
<table width="260" id="tab" cellpadding="1" cellspacing="10">
        <tr>
            <td width="20px"> AccessionNo </td>
            <td width="400px">
                <select name="bkid" onChange="load_bkissue(this.value)" class="txtbox">
            <option value="1">1 </option>
            <option value="2">2 </option>
            <option value="3">3 </option>
            <option value="4">4 </option>
            <option value="5">5 </option>

                        </select>           </td>
        </tr>
        <tr>
            <td> Title </td>

            <td><input name="txtbnam" id="txtbnam" type="text"  class="txtbox"></td>
        </tr>
        <tr>
            <td> Author </td>
            <td><input name="txauth" id="txauth" type="text" id="txauth" class="txtbox"></td>
        </tr>
        <tr>
            <td> Category </td>

            <td><input name="txtcat" id="txtcat" type="text" id="txtcat"  class="txtbox"></td>
        </tr>
        <tr>
            <td> Subject </td>
            <td><input name="txtsub" id="txtsub" type="text" id="txtsub"  class="txtbox"></td>
        </tr>
        <tr>
            <td> Rack </td>

            <td><input name="txtrack" id="txtrack" type="text" id="txtrack" class="txtbox"></td>
        </tr>
        </table>

</body>
</html>

PHP:

<?php
$json = array(
                'accession_id' => "a",
                'title'        => "b",
                'author'       => "c",
                'category'     => "d",
                'subject_type' => "e",
                'rack_no'      => "f");
echo json_encode($json );
?>

选择AccessionNo时,我需要在每个字段(例如标题,作者等)中使用这些值,但没有任何变化。

Your JSON, is...weird. Why not just do it like this:

<?php
$json = array(
                'accession_id' => "a",
                'title'        => "b",
                'author'       => "c",
                'category'     => "d",
                'subject_type' => "e",
                'rack_no'      => "f");
echo json_encode($json);
?>

This results in JSON that looks like this (much more sane):

{
    "accession_id":"a",
    "title":"b",
    "author":"c",
    "category":"d",
    "subject_type":"e",
    "rack_no":"f"
}

And makes your javascript much simpler:

$.ajax({
    type: "POST",
    url: "http://temp.lmfast1/testajax/2.php",
    async: false,
    dataType: "json",
    data: "accession_id=" + accessid,
    success: function (response) {
        $("#bkid").val(response.accession_id);
        $("#txtbnam").val(response.title);
        $("#txauth").val(response.author);
        $("#txtcat").val(response.category);
        $("#txtsub").val(response.subject_type);
        //$("#txt_div").val(response.rack_no);
        $("#txtrack").val(response.rack_no);
    }
});

Your ajax call is missing some important things. Try this call

function load_bkissue(accessid) {
    var myID = { accession_id : accessid };
    var DTO = JSON.stringify(myID);
    $.ajax({
        type: "POST",
        url: "2.php", // /testajax/2.php
        contentType: "application/json",
        dataType: "json",
        data: DTO,
        success: function(response) //'response' is the output provided by the controller method
        {
            alert(response);
            $.each(response, function(i, item) {
                if (item.field == "accession_id") {
                    $("#bkid").val(item.value);
                } else if (item.field == "title") {
                    $("#txtbnam").val(item.value);
                } else if (item.field == "author") {
                    $("#txauth").val(item.value);
                } else if (item.field == "category") {
                    $("#txtcat").val(item.value);
                } else if (item.field == "subject_type") {
                    $("#txtsub").val(item.value);
                } else if (item.field == "rack_no") {
                    //$("#txt_div").val(item.value);
                    $("#txtrack").val(item.value);
                }
            });
        }
    });
}

These are the problems I have seen in your code.

  1. No need to give fully qualified URL as your code may not work in different domain (Same Origin Policy)
  2. You are missing contentType
  3. You have specified dataType: 'json', but your data is not of type json.