Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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/2/jquery/81.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响应读取数组_Php_Jquery_Ajax_Json - Fatal编程技术网

Php jQuery从ajax响应读取数组

Php jQuery从ajax响应读取数组,php,jquery,ajax,json,Php,Jquery,Ajax,Json,这是如此基本,但我现在已经被困了一个多小时,因为我找不到一个确切的例子。感谢您提供的任何链接 我在php中构建JSON,如下所示: return json_encode(['error' => 'customer number not found']); 它返回一个基本数组,如下所示(来自inspect): 我通过jQuery调用了Ajax/getJSON: $.getJSON( url, function( response ) { $.each(respons

这是如此基本,但我现在已经被困了一个多小时,因为我找不到一个确切的例子。感谢您提供的任何链接

我在php中构建JSON,如下所示:

return json_encode(['error' => 'customer number not found']);
它返回一个基本数组,如下所示(来自inspect):

我通过jQuery调用了Ajax/getJSON:

   $.getJSON( url, function( response ) {

        $.each(response[0], function(key, value) {

            // Check response for errors
            if (key == "error") {

                // display message
                $(message_area).innerHTML = value;

                // Show the error message
                $(message_area).show();

            };

        });

    });
我只想检查第一个值,然后显示第二个值并显示容器。上面的代码给了我

Uncaught TypeError: Cannot read property 'length' of undefined

{error:'customer number not found'}
不是数组,它是一个简单的对象。因此,您需要使用
response
而不是
response[0]

另外,
innerHTML
是DOM元素的属性,而不是jQuery对象。使用jQuery对象设置html时,请使用
.html()
函数

使用


{error:'customer number not found'}
不是数组,它是一个简单的对象。因此,您需要使用
response
而不是
response[0]

另外,
innerHTML
是DOM元素的属性,而不是jQuery对象。使用jQuery对象设置html时,请使用
.html()
函数

使用


尝试
response
而不是
response[0]

您只是访问数组的第一个元素,而不是在其上循环


长度错误是因为单个第一项没有长度。

尝试
response
而不是
response[0]

您只是访问数组的第一个元素,而不是在其上循环

长度错误是因为单个第一项没有长度。

在PHP中:

return json_encode(array('error' => 'customer number not found'));
在jQuery.done()函数中:

.done(function(response){
    response = $.parseJSON(response);
    $.each(response, function(key, value) {
        if(key == 'error') {
            // display message
            $(message_area).html(value);

            // Show the error message
            $(message_area).show();
        }
    });
});
在PHP中:

return json_encode(array('error' => 'customer number not found'));
在jQuery.done()函数中:

.done(function(response){
    response = $.parseJSON(response);
    $.each(response, function(key, value) {
        if(key == 'error') {
            // display message
            $(message_area).html(value);

            // Show the error message
            $(message_area).show();
        }
    });
});

我宁愿这样做:

$.getJSON( url, function( response ) {
  for(var key in response) {
    if(!response.hasOwnProperty(key)) continue;
    if(key === "error") {
      // display message
      $(message_area).innerHTML = response[key];
      // Show the error message
      $(message_area).show();
      break; // exit the loop!
    }
  }
});

在这种情况下,它将通过对象的键进行循环,在JSON情况下,它将非常有效。

我更愿意这样做:

$.getJSON( url, function( response ) {
  for(var key in response) {
    if(!response.hasOwnProperty(key)) continue;
    if(key === "error") {
      // display message
      $(message_area).innerHTML = response[key];
      // Show the error message
      $(message_area).show();
      break; // exit the loop!
    }
  }
});

在这种情况下,它将通过对象的键进行循环,而在JSON情况下,它将非常有效。

而不是使用
$。每个
,执行相同操作的更好方法(给定当前场景)是检查
响应。错误
是否存在。:)

将代码更改为

 $.getJSON( url, function( response ) {
    if (response.error){
        $(message_area).html(response.error);
        // Show the error message
        $(message_area).show();
    } else{
        // No error, do your success stuff
    }
 });
增编:

首先,从PHP获得的
响应
不是数组。它是一个物体。检查对象中的现有属性比遍历对象的键和值更明智、更直观

第二,您的
$(消息区域)
是一个jQuery对象。您不能按照设置jQuery对象的方式直接为其设置innerHTML。假设
message\u area
只选择一个元素,那么这样做的方法是
$(消息区域)[0].innerHTML=“您的值”

但如果您使用jQuery,则更明智的做法是遵循jQuery的精神,因此您实际上应该做的是

$(message_area).html(“您的值”)


希望有帮助。:)

不要使用
$。每个
,做同样事情的更好方法(给定当前场景)是检查
响应是否存在。错误
:)

将代码更改为

 $.getJSON( url, function( response ) {
    if (response.error){
        $(message_area).html(response.error);
        // Show the error message
        $(message_area).show();
    } else{
        // No error, do your success stuff
    }
 });
增编:

首先,从PHP获得的
响应
不是数组。它是一个物体。检查对象中的现有属性比遍历对象的键和值更明智、更直观

第二,您的
$(消息区域)
是一个jQuery对象。您不能按照设置jQuery对象的方式直接为其设置innerHTML。假设
message\u area
只选择一个元素,那么这样做的方法是
$(消息区域)[0].innerHTML=“您的值”

但如果您使用jQuery,则更明智的做法是遵循jQuery的精神,因此您实际上应该做的是

$(message_area).html(“您的值”)


希望有帮助。:)

太棒了,非常适合我想要达到的目标。非常感谢!顺便说一句,如果不提供值,您将从php获得一个数组:
json_encode(['one','two'])->
[“一”,“二”]
,否则会得到一个对象。@starikovs:你说得对。然而,我的答案是在OP从服务器得到什么的上下文中。也许,我应该利用你的这一评论作为提高我英语水平的机会当我再次阅读我的答案时,我意识到附录中的第一句话是模棱两可的,可能会让读者认为从PHP永远无法获得数组。感谢您的反馈。:)太棒了,非常适合我想要达到的目标。非常感谢!顺便说一句,如果不提供值,您将从php获得一个数组:
json_encode(['one','two'])->
[“一”,“二”]
,否则会得到一个对象。@starikovs:你说得对。然而,我的答案是在OP从服务器得到什么的上下文中。也许,我应该利用你的这一评论作为提高我英语水平的机会当我再次阅读我的答案时,我意识到附录中的第一句话是模棱两可的,可能会让读者认为从PHP永远无法获得数组。感谢您的反馈。:)