douting1871 2015-07-17 04:38
浏览 130
已采纳

如何在JSON.parsed responseText上保留hasOwnProperty?

I am a very new developer working on a very simple application as part of my training process - so be gentle.

I have built a function in javascript that accepts arbitrary objects from elsewhere and builds a legal POST request string.

Code:

    function postify(oPost){
    var out = '';
    for (key in oPost){
        if (oPost.hasOwnProperty(key) && key >0){
            if(oPost[key].value != 'Submit'){
                out += '&' + oPost[key].name + '=' + oPost[key].value;
            }
        }
    }
    return out;
}

There are many like it, but this one is mine. I elected to use hasOwnProperty as a conditional, as the total list of inherited properties could be really quite long.

One of the objects I would like to pass to this function is a JSON parsed responseText object, which is retrieved like so.

function postData(str){
        var http = new XMLHttpRequest();
        http.open('POST', 'test.php',false);
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", str.length);
        http.setRequestHeader("Connection", "close");
        http.send(str);
        var response = JSON.parse(http.responseText);
        responseHandle(response);
    }

So, the problem - both of these functions do exactly what they are supposed to do, until my responseHandle function routes the response object to the postify function. Manual checking indicates that all expected properties are in place, but postify() won't concatenate a string because those properties seem to have been inherited.

I am fully aware that I could trivially brute force assign all necessary properties - the handler function would do what it needed to either way. I am also aware that my synchronous XMLHttpRequest is deprecated - right this second, it's what I need, and works fine.

So, then, some questions - is there a way to pass my JSON.parsed object such that hasOwnProperty() == true is maintained? Is there a different property or technique I could or should be using in postify() to look for deliberately set key value pairs? Should I just rig the POST to transmit ALL of the inherited properties of the object I am POSTING to PHP?

  • 写回答

2条回答 默认 最新

  • douxun4860 2015-07-17 05:13
    关注

    The problem isn't with hasOwnProperty, it's with key > 0. Unless oPost is an array, the keys will be strings. When you compare a string with a number, the string is converted to a number. But if the string isn't numeric, the conversion will return NaN, and comparing this with 0 is false.

    Your function shouldn't have worked for any object, it doesn't matter if it came from JSON.parse(). When JSON.parse returns an object, all the properties are "own".

    The fix is to change

    if (oPost.hasOwnProperty(key) && key >0){
    

    to

    if (oPost.hasOwnProperty(key)){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B