Php 如何将ajax结果用作可以在页面中的任何位置打印的变量

Php 如何将ajax结果用作可以在页面中的任何位置打印的变量,php,ajax,Php,Ajax,以下是我的ajax代码: $("#to").change(function(){ $.ajax({ type: "POST", url: "<?php echo base_url();?>/index.php/sales/getPrice", dataType: "html", data: {'from' : $('#from').val() , to: $('#to').val()}, success: fu

以下是我的ajax代码:

$("#to").change(function(){
  $.ajax({ 
  type: "POST",
  url: "<?php echo base_url();?>/index.php/sales/getPrice",             
  dataType: "html",       
  data: {'from' : $('#from').val() , to: $('#to').val()},    
  success: function(response){                
  //$(".location").html(response); 
 }
});             
});

您想使用ajax调用返回的数据吗?因此,您可以在
success
部分中使用它:

$("#to").change(function(){
  $.ajax({ 
  type: "POST",
  url: "<?php echo base_url();?>/index.php/sales/getPrice",             
  dataType: "html",       
  data: {'from' : $('#from').val() , to: $('#to').val()},    
  success: function(response){                
     alert(respone.price);
     console.log(response);
 }
});             
});
$(“#to”).change(函数(){
$.ajax({
类型:“POST”,
url:“/index.php/sales/getPrice”,
数据类型:“html”,
数据:{'from':$('from').val(),to:$('from').val(),
成功:功能(响应){
警报(响应价格);
控制台日志(响应);
}
});             
});

使用
console.log(响应)
在success函数中,在浏览器的开发者控制台中获取数据。

调用
getPrice
返回的是
e:…
东西吗?那是什么?它不是json,这意味着您必须自己解析该文本。但一旦解析了它,就可以直接从解析的结果中提取价格值。只有在初始化
e
之前调用了async success函数,这才有效。如何使用php$_POST['result']获得结果;
var result = '';

$("#to").change(function(){
  $.ajax({ 
  type: "POST",
  async: false,
  url: "<?php echo base_url();?>/index.php/sales/getPrice",             
  dataType: "html",       
  data: {'from' : $('#from').val() , to: $('#to').val()},    
  success: function(response){                
      result = response;
 }
}); 
});
var result = '';

$("#to").change(function(){
  $.ajax({ 
  type: "POST",
  async: false,
  url: "<?php echo base_url();?>/index.php/sales/getPrice",             
  dataType: "html",       
  data: {'from' : $('#from').val() , to: $('#to').val()},    
  success: function(response){                
      result = response;
 }
}); 
});
e: {
     price   : result,
     category: 'Economy'
  }