简体   繁体   English

JavaScript / JQuery:对象属性问题

[英]JavaScript/JQuery: Problem with Object Properties

I'm calling the $.ajax function using JQuery. 我正在使用JQuery调用$ .ajax函数。 The php that is called returns a string structured as such "varname=varvalue&varname2=varvalue2". 被调用的php返回一个字符串,其结构如“ varname = varvalue&varname2 = varvalue2”。 I'm breaking it all up and creating an object that uses the varnames as properites and the varvalues as values of those properties. 我将全部分解,并创建一个使用varnames作为属性并且使用varvalues作为这些属性的值的对象。 Here's my code: 这是我的代码:

$.ajax({
           type:"POST",
           url:"/actions/savebbuilderdata.php",
           data:({step: stepNum, submitObj: saveObj}),
           complete:function(jqXHR, textStatus){
               var returnObj = new Object();
               var returnArr = jqXHR.responseText.split("&");
               for(i=0;i<returnArr.length;i++){
                   var tempArr = returnArr[i].split("=");
                   returnObj[tempArr[0]] = tempArr[1];
               }
               //For Testing that the values are infact being assigned
               for(prop in returnObj){
                   alert(prop + ": " + returnObj[prop]);
               }
                alert(returnObj.returnText);
           }
           });

Notice the second for loop. 注意第二个for循环。 I'm simply doing that to get an alert for each property within the object. 我只是在这样做,以获得对象内每个属性的警报。 This works correctly and shows each property name with the correct value. 这可以正常工作,并显示具有正确值的每个属性名称。 One of the properties being returned is always 'returnText'. 返回的属性之一始终是“ returnText”。 However, when I call the final alert attempting to get that returnText I get 'undefined'. 但是,当我调用最后一个警报以尝试获取该returnText时,得到的是“未定义”。 Any ideas why this might be? 任何想法为什么会这样?

Why don't you return the data as a json encoded array instead? 为什么不将数据作为json编码的数组返回呢? Much easier to work with 使用起来容易得多

I can't see much wrong with the code, but maybe these can help you identify the problem. 我看不出代码有多大错,但是也许这些可以帮助您识别问题。 I would use success instead of complete to make sure the problem isn't in the transfer of information. 我将使用success而不是complete来确保问题不在信息传递中。

I also extract the deparameterize code into a separate function because it is both easier to test and seems like something I would do again. 我还提取deparameterize代码到一个单独的功能,因为它是既容易测试,似乎喜欢的事,我会再次这样做。

String.prototype.deparameterize = function() {
    var hsh = new Object;

    var pairs = this.split("&");

    for(i=0;i<pairs.length;i++){
       var pair = pairs[i].split("="); 
       hsh[pair[0]] = pair[1];
    }

    return hsh;
}

$.ajax({
    type:"POST",
    url:"/actions/savebbuilderdata.php",
    data:({step: stepNum, submitObj: saveObj}),
    success:function(data, textStatus,jqXHR){
        var obj = data.deparameterize();
        alert(obj.returnText);
    }
});

Note: not always best practice to extend String, but maybe in this case it works. 注意:扩展String并非始终是最佳实践,但在这种情况下可能有效。

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

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