JQuery从Ajax响应中获取价值

时间:2022-06-10 19:41:30

I use Ajax to call a PHP script. This then returns an array of data. Now if I do the following:

我使用Ajax来调用PHP脚本。然后返回一个数据数组。现在,如果我执行以下操作:

.done(function( response ) {
    if(response === false || response === 'false') {
        $('.main-container').css('display', 'none');
        $('.invalidRow').css('display', 'block');
    } else {
        $('.main-container').css('display', 'block');
        $('#emailAddress').append(response.replace(/['"]+/g, ''));
    }
}).fail(function( jqXHR, textStatus  ) {
    console.log(textStatus);
});

The output is

输出是

array(4) { [0]=> string(19) myemail@email.com [1]=> string(11) 778765 }

array(4){[0] => string(19)myemail@email.com [1] => string(11)778765}

So it outputs the whole response as expected. However, if I change it to

因此它按预期输出整个响应。但是,如果我将其更改为

response[0].replace(/['"]+/g, '')

It only prints out a (from the word array). I am trying to get it to print out myemail@email.com. How can I do this?

它只打印出一个(来自单词数组)。我想把它打印出myemail@email.com。我怎样才能做到这一点?

Thanks

UPDATE My response is now

更新我的回复现在

[myemail@email.com,12345,myname,mysurname]

How do I get these individual values?

我如何获得这些个人价值观?

Thanks

2 个解决方案

#1


1  

You want to parse the JSON string and access its values.

您想要解析JSON字符串并访问其值。

.done(function( response ) {
    if(response === false || response === 'false') {
        $('.main-container').css('display', 'none');
        $('.invalidRow').css('display', 'block');
    } else {
        var jsonObject = JSON.parse(response);
        // then access it like any normal javascript object.
        $('.main-container').css('display', 'block');
        $('#emailAddress').append(response.replace(/['"]+/g, ''));
    }
}).fail(function( jqXHR, textStatus  ) {
    console.log(textStatus);
});

#2


1  

It looks like the PHP Script outputs a var_dump instead of the data. Consider using json_encode() in your PHP function and then on the client side process the response with JSON.parse().

看起来PHP脚本输出var_dump而不是数据。考虑在PHP函数中使用json_encode(),然后在客户端使用JSON.parse()处理响应。

#1


1  

You want to parse the JSON string and access its values.

您想要解析JSON字符串并访问其值。

.done(function( response ) {
    if(response === false || response === 'false') {
        $('.main-container').css('display', 'none');
        $('.invalidRow').css('display', 'block');
    } else {
        var jsonObject = JSON.parse(response);
        // then access it like any normal javascript object.
        $('.main-container').css('display', 'block');
        $('#emailAddress').append(response.replace(/['"]+/g, ''));
    }
}).fail(function( jqXHR, textStatus  ) {
    console.log(textStatus);
});

#2


1  

It looks like the PHP Script outputs a var_dump instead of the data. Consider using json_encode() in your PHP function and then on the client side process the response with JSON.parse().

看起来PHP脚本输出var_dump而不是数据。考虑在PHP函数中使用json_encode(),然后在客户端使用JSON.parse()处理响应。