未捕获的TypeError:无法读取未定义的属性“wsname”

时间:2022-12-02 08:16:35

I'm trying to get values from database using following code (login.js)

我正在尝试使用以下代码从数据库中获取值(login.js)

$.post("http://awebsite.com/app/login.php",{ rep1: rep, password1:password},
function(data) {
if(data=='Invalid rep.......') {
$('input[type="text"]').css({"border":"2px solid red","box-shadow":"0 0 3px red"});
$('input[type="password"]').css({"border":"2px solid #00F5FF","box-shadow":"0 0 5px #00F5FF"});
alert(data);
}else if(data=='Repname or Password is wrong...!!!!'){
$('input[type="text"],input[type="password"]').css({"border":"2px solid red","box-shadow":"0 0 3px red"});
alert(data);
} else if(data !==''){
//$("form")[0].reset();
//$('input[type="text"],input[type="password"]').css({"border":"2px solid #00F5FF","box-shadow":"0 0 5px #00F5FF"});
//alert(data);
alert("Welcome " + rep + "  !!");

var obj = JSON.parse(data);

//[{"wid":"2","repid":"1"}]
 //objwid = obj[0]["wid"];
 //objrepid = obj[0]["repid"];
 objwsname= obj[0]["wsname"];

but I'm getting Uncaught TypeError: Cannot read property 'wsname' of undefined error in this line objwsname= obj[0]["wsname"];

但我得到的是未捕获的TypeError:不能在这一行中读取属性‘wsname’的未定义错误objwsname= obj[0]["wsname"];

I'm sure that getting correct data see this 未捕获的TypeError:无法读取未定义的属性“wsname”

我相信得到正确的数据会看到这个

Am using following scripts in html

在html中使用以下脚本吗

<script src="js/jquery.js"></script>
<script src="js/jquery.mobile-1.0rc1.min.js"></script>
<script src="jqm.autoComplete-1.5.2-min.js"></script>
<script src="src\jquery.tabletojson.js"></script>
<script type="text/javascript" src="js/login.js"></script>

2 个解决方案

#1


0  

Should be just this:

应该是这样的:

objwsname = obj.wsname;

obj is an object not an array.

obj是对象而不是数组。

If the JSON string that is passed to JSON.parse contains an array, then it will return an array. In your case, a string containing a JSON object was passed to JSON.parse - so it returned an object.

如果传递给JSON的JSON字符串。解析包含一个数组,然后它将返回一个数组。在您的示例中,包含JSON对象的字符串被传递给JSON。解析——因此它返回一个对象。

#2


1  

As this is just an object not an array which holds one or more objects, so you don't need to provide the index [0]:

因为这只是一个对象,而不是包含一个或多个对象的数组,所以不需要提供索引[0]:

objwsname = obj["wsname"];

or:

或者:

objwsname = obj.wsname;

can be done to get the wsname.

可以获取wsname。


See if you have an array which holds an object then you have to get it with the index of it like:

如果你有一个包含对象的数组那么你必须用它的索引来获取它,比如:

// suppose this is the data
data = [{"foo":"bar"}, {"foo":"baz"}];
//      ^----0------^  ^-----1-----^  // indexes of the objects inside data array.

so these two:

所以这两个:

alert(data[0].foo); // alerts bar
alert(data[0]['foo']); // alerts bar

here you need to have index.

这里需要有索引。

#1


0  

Should be just this:

应该是这样的:

objwsname = obj.wsname;

obj is an object not an array.

obj是对象而不是数组。

If the JSON string that is passed to JSON.parse contains an array, then it will return an array. In your case, a string containing a JSON object was passed to JSON.parse - so it returned an object.

如果传递给JSON的JSON字符串。解析包含一个数组,然后它将返回一个数组。在您的示例中,包含JSON对象的字符串被传递给JSON。解析——因此它返回一个对象。

#2


1  

As this is just an object not an array which holds one or more objects, so you don't need to provide the index [0]:

因为这只是一个对象,而不是包含一个或多个对象的数组,所以不需要提供索引[0]:

objwsname = obj["wsname"];

or:

或者:

objwsname = obj.wsname;

can be done to get the wsname.

可以获取wsname。


See if you have an array which holds an object then you have to get it with the index of it like:

如果你有一个包含对象的数组那么你必须用它的索引来获取它,比如:

// suppose this is the data
data = [{"foo":"bar"}, {"foo":"baz"}];
//      ^----0------^  ^-----1-----^  // indexes of the objects inside data array.

so these two:

所以这两个:

alert(data[0].foo); // alerts bar
alert(data[0]['foo']); // alerts bar

here you need to have index.

这里需要有索引。