简体   繁体   English

json数据未在jquery中显示

[英]json data not shown in jquery

Html Code is : Html代码是:

<select  name="ser" id="ser" class="form-control" onchange="getPrice(this.value);">
<option value="">--Select--</option>
<option value="Value11">Value1</option>
<option value="Value2">Value2</option>
</select>
<input type="text" name="sale" id="saleprice"  class="form-control" />
<input type="text" name="sale" id="saletax"  class="form-control" />
<input type="text" name="sale" id="avalqty"  class="form-control" />

on my Js Page : 在我的Js页面上:

function getPrice(val)
{
   $.ajax({
     type: 'post',
     url: 'get_sales_price.php',
     data: {
       get_option:val
     },
     success: function (response) {
        var valuesar = response.split("|");
        $('#saleprice').val(valuesar[0]);
        $('#saletax').val(valuesar[1]);
        $('#avalqty').val(valuesar[2]);
     }
   });
}

This is my PHP Page Data: 这是我的PHP页面数据:

$data = array();    
$values=$variable1['value1'].'|'.$variable2['value2'].'|'.$variable3;
array_push($data, $values);
echo json_encode($data);

The value on #saleprice is : ["61.25 and the value on #avalqty is : 155"] and the value on #saletax is : 1. #saletax value is correct.. How to get #saleprice : ["61.25 to 61.25 and #avalqty : 155"] to 155 #saleprice上的值是: [“61.25#avalqty上的值是: 155”] ,而#saletax上的值是:1。 #saletax值是正确的..如何获得#saleprice[“61.2561.25#avalqty155“155

I think what you can do is to return a key-value object from the server and use that in the success handler. 我认为你可以做的是从服务器返回一个key-value对象,并在成功处理程序中使用它。 In your case you are returning a array with a single string value 在您的情况下,您将返回一个具有单个字符串值的数组

$data = array();    
$data['price'] = $variable1['value1'];
$data['tax'] = $variable2['value2'];
$data['qty'] = $variable3;
echo json_encode($data);

then 然后

function getPrice(val) {
  $.ajax({
    type: 'post',
    url: 'get_sales_price.php',
    data: {
      get_option: val
    },
    dataType: 'json',
    success: function(response) {
        console.log(response)
      $('#saleprice').val(response.price);
      $('#saletax').val(response.tax);
      $('#avalqty').val(response.qty);
    }
  });
}

try JSON.parse(response) in your success function. 在你的成功函数中尝试JSON.parse(response)。 You are sending your string in an array, so you need to parse it first, access the string using array index and then split this string. 您是在数组中发送字符串,因此您需要先解析它,使用数组索引访问该字符串,然后拆分此字符串。 Otherwise, you can simply echo the string directly in your php code, rather than pushing it in an array and then using json_encode. 否则,您可以直接在PHP代码中回显字符串,而不是在数组中推送它,然后使用json_encode。

So your php code should be 所以你的PHP代码应该是

$values=$variable1.'|'.$variable2.'|'.$variable3;
echo $data;

This way you will receive a string in your ajax response, which you can split directly. 这样,您将在ajax响应中收到一个字符串,您可以直接拆分。

Change 更改

var valuesar = response.split("|"); to

 var valuesar = response[0].split("|");

hey you can use your code in following way 嘿,您可以按照以下方式使用您的代码

<?php
$data = array();    
$values='6.15'.'|'.'52.22'.'|'.'55.25';
array_push($data, $values);

$rr=json_encode($data);


$ltrim = ltrim($rr, '["');
$rtrim = rtrim($ltrim, '"]');


// echo $rtrim;



?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">

jQuery(document).ready(function() {


var response='<?php echo  $rtrim ?>';
console.log(response);

var valuesar  = response.split("|");


$('#saleprice').val(valuesar[0]);
$('#saletax').val(valuesar[1]);
$('#avalqty').val(valuesar[2]);

 console.log(valuesar[0]);

 });

 </script>
 <!DOCTYPE html>
 <html>
 <body>

<form>
<input type="text" name="saleprice" id="saleprice" >
<input type="text" name="saletax" id="saletax" >
<input type="text" name="avalqty" id="avalqty" >
</form> 

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM