值不会传递到下一页

时间:2014-10-29 07:08:51

标签: javascript jquery url

我正在尝试将变量userId传递给我的第二页。

以下是我用来将值传递给下一页的代码:

$.each($.parseJSON(data), function(key, value) {
    var userId = value.id_user;
    var stText = value.status_text;
    if (stText == "OK") {                  
        //if provide a valid username change the location 
        //to the specified page.
        window.location = "http://myurl.com/profile.php";
        window.location.href = "http://myurl.com/profile.php?userId=" + userId;
    }
});

但网址看起来像是:

http://myurl.com/profile.php?userId=undefined

2 个答案:

答案 0 :(得分:0)

这应该尝试这个,

var result = jQuery.parseJSON(data);
$.each(result.data, function(key, value) {
    var userId = value.id_user;
    var stText = value.status_text;
    if (stText == "OK") {                  
        //if provide a valid username change the location 
        //to the specified page.
        window.location = "http://myurl.com/profile.php";
        window.location.href = "http://myurl.com/profile.php?userId=" + userId;
    }
});

答案 1 :(得分:0)

这可能会起作用试试这个

var data='{"header":{"status":"200","status_text":"OK"},"data":{"id_user":"38"}}';
    var result = jQuery.parseJSON(data);
    $.each(result.data, function(key, value) {
        var userId = value.id_user;
        var stText = value.status_text;
        if (stText == "OK") {                  
            //if provide a valid username change the location 
            //to the specified page.
           // window.location = "http://myurl.com/profile.php";
            window.location.href = "http://myurl.com/profile.php?userId=" + userId;
        }
    });