如何将数组发送到查询字符串

时间:2015-03-17 11:51:34

标签: javascript html angularjs

  

朋友们,我需要帮助将数组发送到查询字符串。只是一个简单的html页面。

var obj = [];
    obj[0] = {Guest:"Ramkumar", City: "Madurai", Mobile: "9578606320", Email: "bsrsms@gmail.com", Address:"first street" };
    obj[1] = {Guest:"Sathish", City: "Madurai", Mobile: "9578606320", Email: "bsrsms@gmail.com", Address:"first street" };
    obj[2] = {Guest:"Suresh", City: "Madurai", Mobile: "9578606320", Email: "bsrsms@gmail.com", Address:"first street" };
    obj[3] = {Guest:"Ganesh", City: "Madurai", Mobile: "9578606320", Email: "bsrsms@gmail.com", Address:"first street" };

var x = JSON.stringify(obj);


window.location = "view.html?Object=" + JSON.stingify(obj);
  

重定向时我得到了这样的错误.. 内部服务器错误。

2 个答案:

答案 0 :(得分:1)

var obj = [];
    obj[0] = {Guest:"Ramkumar", City: "Madurai", Mobile: "9578606320", Email: "bsrsms@gmail.com", Address:"first street" };
    obj[1] = {Guest:"Sathish", City: "Madurai", Mobile: "9578606320", Email: "bsrsms@gmail.com", Address:"first street" };
    obj[2] = {Guest:"Suresh", City: "Madurai", Mobile: "9578606320", Email: "bsrsms@gmail.com", Address:"first street" };
    obj[3] = {Guest:"Ganesh", City: "Madurai", Mobile: "9578606320", Email: "bsrsms@gmail.com", Address:"first street" };

var x = JSON.stringify(obj);


window.location = "view.html?Object=" + JSON.stingify(obj);

包含错误。

window.location = "view.html?Object=" + JSON.stingify(obj);

应该是

window.location = "view.html?Object=" + JSON.stringify(obj);

JSON。 stringify ,而不是JSON。 stingify

您的服务器端可能不会期望代码输出错误。

修改

如评论中所述,您已将对象字符串化为变量x,因此无需再次对其进行字符串化。因此你也可以使用

window.location = "view.html?Object=" + x;

答案 1 :(得分:0)

尝试使用encodeURIComponent正确编码网址。

var x = encodeURIComponent(JSON.stringify(obj));
window.location = "view.html?Object=" + x;

除了stingify拼写错误之外,它应该是stringify

此外,由于GET变量似乎非常大,您可能需要考虑切换到POST。我建议您查看maximum length of HTTP GET request?