繁体   English   中英

AJAX代码在PHP中不起作用

[英]AJAX code doesn't work in 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 ,我需要在每个字段(例如标题,作者等)中具有这些值。但这是行不通的。

您的JSON很奇怪。 为什么不这样做:

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

这会导致JSON看起来像这样(更加理智):

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

并使您的JavaScript 更加简单:

$.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);
    }
});

您的ajax调用缺少一些重要的东西。 试试这个电话

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);
                }
            });
        }
    });
}

这些是我在您的代码中看到的问题。

  1. 无需提供完全限定的URL,因为您的代码可能无法在其他域中使用( 相同原始策略
  2. 您缺少contentType
  3. 您指定了dataType: 'json' ,但是您的数据不是json类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM