gpt4 book ai didi

javascript - jQuery 成功不会在代码 200 上触发

转载 作者:行者123 更新时间:2023-11-29 19:10:01 26 4
gpt4 key购买 nike

我有下一个 PHP 代码:

    $servername = "***";
$username = "***";
$password = "***";
$dbname = "***";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "select * from `opencartdb`.`oc_design` where name='mydesign '";
$res=$conn->query($sql);
$records=$res->fetch_assoc();//success

$result2=$records['info'];// already json
$conn->close();


echo ($result2);

我尝试用下一个 JS 捕捉它:

jQuery.ajax({
dataType: "json",
url: "ajax.php?type=loadDesign&user_id="+user_id+"&design_id="+key
}).success(function( data ) {
alert(0);
console.log((data));
}).always(function(){
alert(1);
});

最后我看到 alerted 1调试器显示下一个结果。回应:

{"vectors":"{"front":{"0":{"type":"text","width":"55px","height":"27px","top":"151px","left":"86px","zIndex":"1","svg":"<svg width=\"54.9375\" height=\"27.09375\" viewBox=\"0 0 54.9375 27.09375\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><g id=\"0.5646970888306251\"><text fill=\"#FF0000\" stroke=\"none\" stroke-width=\"0\" stroke-linecap=\"round\" stroke-linejoin=\"round\" x=\"\" y=\"\" text-anchor=\"start\" font-size=\"24px\" font-family=\"arial\" data-textcurve=\"1\" data-itemzoom=\"1 1\" data-textspacing=\"0\"><textPath xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:href=\"http://138.68.62.219/Design-Apple-T-shirt-PC55#textPath-item-0\"><tspan dy=\"0\">Hello</tspan></textPath></text></g><defs><path id=\"textPath-item-0\" d=\"M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764\"></path></defs></svg>","rotate":0,"text":"Hello","color":"#FF0000","fontFamily":"arial","align":"center","outlineC":"none","outlineW":0}},"back":{}}","teams":"{}","fonts":"","design_id":"1475007569235335440","image":"uploaded/2016/09/design-1475007569235335440.png","parent_id":"0","product_id":"178","product_options":"000000","title":"","description":""}

这行是在调试中绘制的:

<svg width=\"54.9375\...

最佳答案

设置php中的响应类型为json。否则 jQuery 可能不接受响应类型并且不会触发成功函数。

header('Content-Type: application/json'); 
echo json_encode($result2);

您还应该添加一个字符集以避免 BOM:

header('Content-type:application/json;charset=utf-8');

关于javascript - jQuery 成功不会在代码 200 上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39734659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com