代码以JSON格式返回数据,它包含大约十个值,其中一个值称为state。我想创建函数来比较状态的返回值,例如,如果state = one show image1并且state = 2 show image2。怎么做?
JQuery url
var RealDataApi = function () {
var url_getgatedata = "api/realdata/getgatedata";
// public functions
return {
getGateData: function (done, fail, always) {
var jqxhr = $.get(url_getgatedata);
jqXhrHandler(jqxhr, done, fail, always);
},
JSON数据:
{"gateNo":1,"fullOpen":false,"fullClose":false,"opening":false,"closing":false,"mode":null,"state":null,"cond":null,"positionM":null,"positionP":null}
答案 0 :(得分:1)
getParameterByName(data);//get parameter name state
function getParameterByName(data) {
if (data.state == '2') {
alert('2');
} else {
alert(data.state);
}
}
命名为json数据,然后你可以获得像data.state
这样的状态值,然后将它与你想要比较的值进行比较
答案 1 :(得分:0)
请试试这个:
if (jqxhr.state == 1) {
//Do whatever you wanna do...
} else if (jqxhr.state == 2) {
//Do whatever you wanna do...
}
答案 2 :(得分:0)
对json响应使用eval:
function getState(data)
{
if(data.state==1)
return "image1";
else if(data.state==2)
return "image2";
}
var jqxhr=eval({"gateNo":1,"fullOpen":false,"fullClose":false,"opening":false,"closing":false,"mode":null,"state":1,"cond":null,"positionM":null,"positionP":null});//parse as js object or eval($.get(url_getgatedata));
alert(getState(jqxhr));