【问题标题】:Unable to pass the data to php function using Ajax in jquery无法在 jquery 中使用 Ajax 将数据传递给 php 函数
【发布时间】:2018-12-24 16:17:03
【问题描述】:

我无法在 jquery 中使用 ajax 将数据传递给 PHP 函数。

function updateinventory(product_code,user_id,id,outlet_id) {


var qtyid = 'qty_'+outlet_id;
//alert(product_code);

var dataString = {pcode:product_code};

var newqty = $('#qty_'+outlet_id).val(); 

//alert(newqty);

jQuery.ajax({
url: "<?=base_url()?>inventory/updateInventoryQtyFromProduct",
data: dataString,
type: "POST",
dataType: 'json',
success:function(data){

    console.log("success"+data);


 },
error:function (error){

    console.log(error);
}

 });

 }

PHP 函数是我试图返回 $pcode 的地方,只是为了检查数据是否在此处传递。

public function updateInventoryQtyFromProduct()
{

 $pcode=$_POST['pcode'];
// $us_id=$_POST['user_id'];

 return $pcode;

}  

【问题讨论】:

  • 先接收请求。例如:if(isset($_POST['pcode']))
  • 仍然出现错误
  • 错误是什么?
  • {readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, ...} abort : ƒ (e) always : ƒ () catch : ƒ (e) done : ƒ()失败:ƒ()getAllResponseHeaders:ƒ()getResponseHeader:ƒ(e)overrideMimeType:ƒ(e)管道:ƒ()进度:ƒ()承诺:ƒ(e)readyState:4 responseText:“” setRequestHeader: ƒ (e,t) state : ƒ () status : 200 statusCode : ƒ (e) statusText : "OK" then : ƒ (t,r,i) proto : Object
  • @EmptyBrain 错误函数结果

标签: php jquery ajax jquery-ui


【解决方案1】:

你必须像这样通过:

  public function updateInventoryQtyFromProduct()
    {

     $pcode=$_POST['pcode'];
     echo json_encode($pcode);
     or  
     echo json_encode(array('pcode'=>$pcode)); //if array need

    }  

【讨论】:

    猜你喜欢
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2017-03-23
    • 2015-08-21
    相关资源
    最近更新 更多