尝试在jquery中将db result捕获为json

eeq64g8w  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(294)

当用户单击复选框时,我尝试使用jquery从数据库中获取一些更新的数据。
复选框如下所示:

echo '<input type="checkbox" name="r571" value="1" '. $checked . $disabled .' id="check_kredit" onClick="ds.r571a.value=this.checked ? \''.date('d-m-Y').'\' : \'\';">'; ?>

ajax就像:

$(document).ready(function() {

$("#check_kredit").click(function() {                
      $.ajax({    //create an ajax request to display.php
        type: "GET",
        url: "getkredit.php?k=129",             
        dataType: "json",   //expect json to be returned                
        success: function(response){
            console.log(response);
            //json_data = jQuery.parseJSON(response);
            //alert(response);
            //$("#kreHint").html(response); 
            //console.log("sfsd");
            //console.log(response);
            //alert(response);
        }

    });
});
});

对数据库的调用(getkredit.php)是:

header('Content-Type: application/json', true);
$k = intval($_GET['k']);

if (!$con) {
    die('Could not connect: ' . mysql_error($con));
 }

$kid = "SELECT * FROM kunde WHERE id = '".$k."'";
$rkid = mysql_query($kid) or die(mysql_error);

while($row = mysql_fetch_assoc($rkid)) {
    echo json_encode($row);
}
mysql_close($con);

如果我直接在浏览器中运行php文件,它将返回以下内容:

{
  "id": "129",
  "kundenr": "00000001",
  "hra": "",
  "navn1": "firmanavn",
  "navn2": "",
  "cvr": "142570220805205",
  "adresse1": "Et vejnavn",
  "adresse2": "",
  "postland": null,
  "postnr": "",
  "postby": "Et sted i DK",
  "land": "DK",
  "tlf": "00000000",
  "fax": "00000000",
  "telex": "",
  "www": "",
  "email": "",
  "att": "Hans Nielsen, Gunner Nu Hansen",
  "note": "Faktura skal p\u00e5f\u00f8res f\u00f8lgende: Sagsnr-afdelingsnr-art-evt. valgfri",
  "betal": "Netto kontant, 8 dage",
  "sikker": "Beh\u00f8rig kreditvurdering",
  "kundegruppe": "Entrepren\u00f8r",
  "ansvarlig": "",
  "opret": "2005-01-20 13:07:44",
  "rettet": "2011-08-29 09:59:24",
  "ejer_id": "129",
  "x-factor": "100",
  "kreditmax": "0",
  "kreditbelastning": "0"
}

但是当我通过ajax调用运行它时,它返回html代码。
这让我很困惑,但这可能是我第二次使用ajax了,这对我来说都是新鲜事。那我做错什么了?

tag5nh1u

tag5nh1u1#

将ajax调用更新为:

$(document).ready(function() {

    $("#check_kredit").click(function() {                
          $.ajax({    //create an ajax request to display.php
            type: "GET",
            url: "getkredit.php?k=129",             
            dataType: "json",   
            headers: {         
                       Accept : "application/json; charset=utf-8",         
                       "Content-Type": "application/json; charset=utf-8"  
                     },               
            success: function(response){
                console.log(response);
                //json_data = jQuery.parseJSON(response);
                //alert(response);
                //$("#kreHint").html(response); 
                //console.log("sfsd");
                //console.log(response);
                //alert(response);
            }

        });
    });
    });
mo49yndu

mo49yndu2#

答案是我需要的url是这样的:`url:“\getkredit.php?k=129”

相关问题