Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从jQuery AJAX表单post获取未定义的响应_Php_Javascript_Jquery_Ajax - Fatal编程技术网

Php 从jQuery AJAX表单post获取未定义的响应

Php 从jQuery AJAX表单post获取未定义的响应,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,需要将#sku设置为隐藏表单字段的值(不确定我在上面的jQuery代码中是否正确执行了此操作) $(document).ready(function() { $('#pricingEngine').change(function() { var query = $("#pricingEngine").serialize(); $('#price').fadeOut(500).addClass('ajax-loading'); $.aj

需要将#sku设置为隐藏表单字段的值(不确定我在上面的jQuery代码中是否正确执行了此操作)

$(document).ready(function() {
    $('#pricingEngine').change(function() {
         var query = $("#pricingEngine").serialize();
         $('#price').fadeOut(500).addClass('ajax-loading');
         $.ajax({
             type: "POST",
             url: "index.php/welcome/PricingEngine",
             data: query,
             dataType: 'json',
             success: function(data)
             {
               $('#price').removeClass('ajax-loading').html('$' + data.F_PRICE).fadeIn(500);
               $('#sku').attr('value') = (data.businesscards_id);
             }
         });
    return false;
   });
});
但是我在价格框中只得到了“未定义的”。这是什么原因?

这是正确的方法(最好的)

如果您坚持使用attr,这应该是可行的

$('#sku').val(data.businesscards_id);

作为JSON返回的结构是一个数组
[]
,其中包含一个元素,该元素是您要定位的对象
{}
。请通过其数组索引
[0]

$('#sku').attr('value', data.businesscards_id);
您还可以
.shift()
从数组中删除第一个元素,并按原样使用它:

// Access the array-wrapped object via its [0] index:
$('#price').removeClass('ajax-loading').html('$' + data[0].F_PRICE).fadeIn(500);
// Likewise here, and set the value with .val()
$('#sku').val(data[0].businesscards_id);

是的,刚刚意识到数据=数据[0];但是我为什么需要它呢?@fabio你说为什么需要它是什么意思?你的AJAX调用返回一个数组。如果你想让它只返回单个对象,你需要在服务器端修改它。好的,我明白为什么我现在必须这么做了。我不知道JSON格式。但是代码部分工作。$('#sku').val(数据[0].businesscards_id);未设置隐藏字段输入的值..有什么想法吗?谢谢帮助!:)@fabio它将值设置为什么
.val()
自1.0以来一直在jQuery中。您使用的是超旧版本吗?没有最新版本的google cdn——它没有设置任何内容,也没有控制台错误:这里是源代码
只需使用
shift()
在ajax响应/成功方法中,n代码可以正常工作。:)
$('#sku').attr('value', data.businesscards_id);
// Access the array-wrapped object via its [0] index:
$('#price').removeClass('ajax-loading').html('$' + data[0].F_PRICE).fadeIn(500);
// Likewise here, and set the value with .val()
$('#sku').val(data[0].businesscards_id);
// Pull the first element off the array, into the same variable
// WARNING: Use a different variable if the array has multiple elements you need to loop over later.
// You *don't* want to do it this way if the array contains multiple objects.
data = data.shift();
$('#price').removeClass('ajax-loading').html('$' + data.F_PRICE).fadeIn(500);
$('#sku').val(data.businesscards_id);