我使用ajax调用并发送“itemShape”作为数据,但在url页面中显示未定义的索引:itemShape

时间:2017-08-10 09:44:35

标签: javascript php jquery ajax

你好我用ajax函数发送数据: itemShape 当我直接运行url页面或服务页面时显示错误:

  

注意:未定义索引:itemShape in   第5行的C:\ wamp64 \ www \ inventory_software \ get_item_types_dhk.php

请某人帮忙解决这个问题。我将上传错误图片以便更好地理解:

enter image description here

这是url页面代码:

<?php
include 'config.php';


    $itemShape = $_POST["itemShape"];

    $mysql="SELECT product_id,item_shape from product_table where item_name="."'$itemShape'";
    //echo $mysql;
    $count=0;
    $myquantity = $conn->query($mysql);
    $new_array=array();
if ($myquantity->num_rows > 0) {
        while($myrow = $myquantity->fetch_assoc()) {
            $quantity_sum['count']=$count+1;
            $quantity_price['item_name']= $myrow['item_shape'];
            $quantity_price['item_id']= $myrow['product_id'];
            array_push($new_array,$quantity_price);


        }
    }
    $new_item=array_merge($quantity_sum,$new_array);
    echo json_encode($new_item);
?>

这是使用的ajax方法:

$('#item_name').change(function(){
        //alert();
        var Item_Name=$("#item_name option:selected").text();
        alert("item_name");
         $.ajax({
         type: 'POST',
         url: 'get_item_types_dhk.php', 
         data: {itemShape: Item_Name},
         dataType: "json",
         success: function(data){ 

有人请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

您将数据作为json发送,而不将contentType设置为json。

解决方案1: 以默认格式发送数据:

而不是data: {itemShape: Item_Name},

data: "itemShape=" + Item_Name,

解决方案2: 设置json的内容类型:

dataType设置为json意味着数据已接收,而不是您要发送的数据;要设置发送数据的数据类型,请使用contentType,如下所示:

$.ajax({
  type: 'POST',
  url: 'get_item_types_dhk.php',   
  contentType: "application/json; charset=utf-8", //set data to be json
  // data: {itemShape: Item_Name}, // before
  data : JSON.stringify({itemShape: Item_Name}), //after
  dataType: "json", // this is the returned data type...
  success: function(data)

阅读更详细的答案dataTypecontentType之间的差异What is content-type and datatype in an AJAX request?