简体   繁体   English

JavaScript代码无法正确检查ajax请求

[英]JavaScript code not checking ajax request properly

I have a case of the Thursdays and I am wondering why this isn't working. 我有一个星期四的事,我想知道为什么这不起作用。

The Problem: I cannot return the value of an array from AJAX request even though the page returns it successfully. 问题:即使页面成功返回数组,我也无法从AJAX请求返回数组的值。

So, here's my AJAX request: 因此,这是我的AJAX请求:

    $.ajax({
    type: "GET",
    url: "get-sendgrid-info.php?username=" + emailToCheck,
        success: function(dataSG) {
            console.log("Length: " + dataSG.length + " Username: " + dataSG[0].username);

            if (dataSG[0].username) {
                console.log('CURRENTLY IN SEND GRID!');
                $(".sgstatus").html("<div style='font-weight:bold;color:green;'>CURRENTLY IN SENDGRID</div>");
            }else{
                console.log('NOT IN SEND GRID!');
                $(".sgstatus").html("<div style='font-weight:bold;color:red;'>NOT IN SENDGRID</div>");
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(errorThrown);
        }
    });

and that dataSG will call that php page: dataSG将调用该php页面:

    if ($email) echo json_encode($data);
}
$stmt->close();
$mysqli->close();

which will output something like this: 这将输出如下内容:

Array
(
    [0] => stdClass Object
        (
            [username] => sample@email.net
            [email] => sample@email.net
            [active] => true
            [first_name] => John
            [last_name] => Doe
            [address] => 123 Fake Street
            [address2] => Suite117
            [city] => Denver
            [state] => CO
            [zip] => 12345
            [country] => US
            [phone] => 555-555-5555
            [website] => http://website.com
            [website_access] => true
        )

)
1

(yes, even that 1 ). (是的,即使是1 )。

So, when I try this after the AJAX request 所以,当我在AJAX请求后尝试此操作时

        if (dataSG[0].username) {
            console.log('CURRENTLY IN SEND GRID!');
            $(".sgstatus").html("<div style='font-weight:bold;color:green;'>CURRENTLY IN SENDGRID</div>");
        }else{
            console.log('NOT IN SEND GRID!');
            $(".sgstatus").html("<div style='font-weight:bold;color:red;'>NOT IN SENDGRID</div>");
        }

I always get NOT IN SENDGRID even though the response shows an array with a username clearly in it. 即使响应显示的数组中清楚地包含用户名,我也总是NOT IN SENDGRID中。

Help, please? 请帮助?

edit: I should add that I am on a IIS server. 编辑:我应该添加我在IIS服务器上。

edit: Response console says: 编辑:响应控制台说:

Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}abort: 
...

Object
create-email.php:2629 SyntaxError: Unexpected token A {stack: (...), message: "Unexpected token A"}message: "Unexpected token A"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: Error

Try this: 尝试这个:

...
success: function(dataSG) {
    dataSG = JSON.parse(dataSG);
...

I think the reason is that your pho script is echoing the string start with "Array". 我认为原因是您的pho脚本正在回显以“ Array”开头的字符串。 The Ajax .get method does a smart guess for return object. Ajax .get方法对返回对象进行智能猜测。 When it receive a string from php, it could not convert it into either Jason nor xml so it think the dataSG is simply string. 当它从php接收到一个字符串时,它无法将其转换为Jason或xml,因此它认为dataSG只是字符串。 The Json_encode did not do it successfully. Json_encode没有成功完成。 You have to format your php output to be something like "['a','b']", then Ajax can successfully convert it into a JavaScript array. 您必须将php输出的格式设置为“ ['a','b']”,然后Ajax才能成功将其转换为JavaScript数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM