jsonというレスポンスからデータを取得しようとしています。しかし、私はエラー応答を得ています。私は一度この問題を抱えていましたが、私はPHPファイルにheader('Content-type: application/json');
を追加して解決することができます。しかし、今私はそれを行う場合、私のコードを壊すのは、私はそれのHTMLコードをたくさん持っている別のファイルにファイルを含めるので、それは応答でコードをロードします。どういうわけか、そのヘッダなしでjsonの応答を得ることはできますか?コンテンツタイプが正しくないAjax JSON
$('#catlist').change(function() {
var opt = $(this).val();
console.log(opt);
$.ajax({
url: 'includes/processproducts.php',
type: 'get',
dataType: "json",
data: {opt: opt},
success: function(options){
console.log(options);
},
error: function(options){
console.log(options);
console.log("not working");
}
});
$('#brandlist').attr('disabled', false);
});
<?php
if(!isset($_SESSION)){
session_start();
}
//
include '../../core/initialize.php';
/*
include 'includes/head.php';
include 'includes/navigation.php';
include 'includes/functions.php';
*/
$sql = "SELECT * FROM product";
$result = $db->query($sql);
if(isset($_REQUEST['submitbtn'])){
if(isset($_REQUEST['edit'])){
?>
<script> alert("EDIT") </script>
<?php
if(!empty($_POST['handleName'])){
$nameEdVar = $_POST['handleName'];
} else {
$nameEdVar = NULL;
}
if(!empty($_POST['handleCat'])){
$catEdVar = $_POST['handleCat'];
} else {
$catEdVar = NULL;
}
if(!empty($_POST['handleBrand'])){
$brandEdVar = $_POST['handleBrand'];
} else {
$brandEdVar = NULL;
}
if(!empty($_POST['handleDesc'])){
$descEdVar = $_POST['handleDesc'];
} else {
$descEdVar = NULL;
}
if(!($_FILES['image']['size'] == 0)){
$path = processImg($_REQUEST['edit']);
} else {
$path = NULL;
}
//$path = NULL;
$edit_id = (int)$_REQUEST['edit'];
$sqled = "UPDATE product SET name='$nameEdVar', cat_id='$catEdVar', brand_id='$brandEdVar', image='$path', description='$descEdVar' WHERE product.id_P = $edit_id";
$db->query($sqled);
//header('Location: products.php');
}else{
if(($_POST['handleName'] =='') || ($_POST['handleCat'] == '')
|| ($_POST['handleBrand'] == '') || ($_POST['handleDesc'] == '')
|| ($_FILES['image']['size'] == 0)){ ?>
<script> alert('ADD časť niečo nevyplnene'); </script>
<?php
}else{
?> <script> alert('ADD časť všetko vyplnene'); </script>
<?php
$nameVar = $_POST['handleName'];
$catVar = $_POST['handleCat'];
$brandVar = $_POST['handleBrand'];
$descVar = $_POST['handleDesc'];
$qVar = 1;
//$path = "https://localhost/shop/imgs/31.jpg";
$sql = "SELECT MAX(id_p) FROM product";
$stmt = $db->prepare($sql);
$stmt->execute();
$stmt->bind_result($maxid);
$stmt->fetch();
$path = processImg(++$maxid);
$stmt->close();
$sql = "INSERT INTO product (id_p, name, cat_id, brand_id, image, description, quantity) VALUES(?, ?, ?, ?, ?, ?, ?)";
$stmt = $db->prepare($sql);
$stmt->bind_param('sssssss', $nullvar = NULL, $nameVar, $catVar, $brandVar, $path, $descVar, $qVar);
$stmt->execute();
$stmt->close();
//header('Location: products.php');
}
}
}
//Delete Product
if(isset($_GET['delete']) && !empty($_GET['delete'])){
$delete_id = (int)$_GET['delete'];
$sqldel = "DELETE FROM product WHERE product.id_p = $delete_id";
$stmt = $db->prepare($sqldel);
$stmt->execute();
$stmt->close();
header('Location: products.php');
}
if(isset($_REQUEST['edit'])){
echo "
<script>
$(document).ready(function() {
$('#addBox').modal('show');
})
</script>";
}
$sqlcat = "SELECT * FROM category";
$stmtcat = $db->prepare($sqlcat);
$stmtcat->execute();
$stmtcat->bind_result($cat_id, $type);
$stmtcat->store_result();
$cat = array();
while($stmtcat->fetch()){
$cat[$cat_id] = array();
$cat[$cat_id]["type"] = $type;
}
$stmtcat->close();
$sqlbrand = "SELECT * FROM brand";
$stmtbrand = $db->prepare($sqlbrand);
$stmtbrand->execute();
$stmtbrand->bind_result($brand_id, $name);
$stmtbrand->store_result();
$brand = array();
while($stmtbrand->fetch()){
$brand[$brand_id] = array();
$brand[$brand_id]["name"] = $name;
}
$stmtbrand->close();
if(isset($_REQUEST['submitcategory'])){
$sql = "INSERT INTO category (cat_id, type) VALUES(?, ?) ON DUPLICATE KEY UPDATE cat_id = cat_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $cat_id = NULL, $_REQUEST['category']);
$stmt->execute();
$stmt->close();
foreach ($_REQUEST as $key=>$value){if($key != "category" && $value != "Submit"){
if($value == ""){
exit();
}
var_dump($value);
$sql = "INSERT INTO brand(brand_id, NAME) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $brand_id = NULL, $value);
$stmt->execute();
$stmt->close();
$sql = "SELECT brand_id, cat_id FROM brand,category WHERE brand.name= ? AND category.type = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('ss', $value, $_REQUEST['category']);
$stmt->execute();
$stmt->bind_result($brand_id, $cat_id);
$stmt->fetch();
$stmt->close();
$sql = "INSERT INTO brand_category(brand_id, cat_id) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $brand_id, $cat_id);
$stmt->execute();
$stmt->close();
}
}
}elseif(isset($_REQUEST['submitbrand'])){
$sql = "INSERT INTO brand (brand_id, name) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $brand_id = NULL, $_REQUEST['brand']);
$stmt->execute();
$stmt->close();
foreach ($_REQUEST as $key=>$value){if($key != "brand" && $value != "Submit"){
if($value == ""){
exit();
}
$sql = "INSERT INTO category (cat_id, type) VALUES(?, ?) ON DUPLICATE KEY UPDATE cat_id = cat_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $cat_id = NULL, $value);
$stmt->execute();
$stmt->close();
$sql = "SELECT brand_id, cat_id FROM brand,category WHERE brand.name= ? AND category.type = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('ss', $_REQUEST['brand'], $value);
$stmt->execute();
$stmt->bind_result($brand_id, $cat_id);
$stmt->fetch();
$stmt->close();
$sql = "INSERT INTO brand_category(brand_id, cat_id) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $brand_id, $cat_id);
$stmt->execute();
$stmt->close();
}
}
}
$sql = "SELECT brand.name, brand.brand_id FROM brand JOIN brand_category ON brand_category.brand_id = brand.brand_id WHERE brand_category.cat_id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $_REQUEST['opt']);
$stmt->execute();
$stmt->bind_result($name, $brand_id);
$stmt->store_result();
$options = array();
echo $name;
echo $brand_id;
while($stmt->fetch()){
$options[$brand_id] = array();
$options[$brand_id]["name"] = $name;
}
//header('Content-type: application/json');
echo json_encode($options);
$stmt->close();
?>
どのようなエラーが発生しますか? –
さて、それは単に 'success:function()'の代わりに 'error:function(options)'にajaxに入っています。 – rtom
すべてのhtmlコードを変数に格納してからjson_encode変数に入れてみてください。 –