奇怪的jquery ajax quirk,返回对象是正确的,属性是未定义的

Strange jquery ajax quirk, return object is correct, properties are undefined

本文关键字:属性 未定义 对象 ajax quirk 返回 jquery      更新时间:2023-09-26

我正在寻找关于这个问题的几个小时,阅读了很多关于 a 和 so 的内容,但无法弄清楚我做错了什么。

我的返回对象数据已正确注销,但一旦我尝试访问属性,它总是未定义的????怎么来了?

我试图在一些SO帖子中data = JSON.parse(JSON.stringify(data))作为建议的答案,但我的问题仍然存在。

欢迎所有帮助!

顺便说一句:

    <script>
        $(document).ready(function(){
                $("#btw").on('focusout',function () {
                    console.log("BTW validation check. (REST CALL)");
                    var btw = $(this).val();
                    var country_code = btw.substring(0,2);
                    var vat_number = btw.substring(2);
                    console.log("country_code: " + country_code);
                    console.log("vat_number: " + vat_number);
                    $.getJSON('http://vatid.eu/check/'+ country_code +'/'+vat_number, function(data) { 
                        console.log(data); // good object with valid set
                        console.log(data["valid"]); //undefined!
                        console.log(data.valid); //undefined!
                        /*
                        if(data.valid) {
                            $("#order_custom_10").value = true
                        }
                        else {
                            $("#order_custom_10").value = false
                        }
                        */
                    });
                });
        });
    </script>
</body>

请求返回的data对象具有包含所需数据的response对象。所以用data.response.valid来得到你想要的。

这是 vatid.eu 中的 JSON 对象结构:

{
  "response": {
      "country_code": "DK",
      "vat_number": "30505166",
      "valid": "true",
      "name": "JUSTABOUTIT ApS",
      "address": "Gammeltorv 8 2'n1457 København K'n"
    }
}