2016-09-19 48 views
0

我想从Ajax响应获取数据作为json。但我收到错误响应。我有这个问题一次,我设法通过添加header('Content-type: application/json');到我的PHP文件来解决它。但是,如果我现在这样做,它会破坏我的代码,因为我将文件包含到其中有很多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(); 

?> 
+2

你得到了什么错误? –

+0

那么,它只是去ajax'错误:函数(选项)'而不是'成功:function()' – rtom

+0

尝试存储所有的HTML代码在一个变量,然后json_encode该变量。 –

回答

0

从Ajax调用删除以下行:

dataType: "json" 

和改线

success: function(options){ 
    var data = JSON.parse(options); // convert the json to js array and use it  
} 

试试这个,它会工作

+0

我得到以下错误'sample.js:122 Uncaught TypeError:JSON.response不是函数' – rtom

+0

请检查更新的代码。它的JSON.parse()。对不起:( –

+0

'VM1037:1未捕获的SyntaxError:位于0位置的JSON中的意外标记W'对象中的第一个词是Windows,因此W很可能属于它 – rtom

0

解决它由刚创建的文件为ajax调用。