AJAX post请求,包含数组和其他对象的对象,无法正确解析为rails

时间:2010-04-05 19:30:25

标签: javascript ruby-on-rails json

我想要的是获得一个合适的参数,如果您看到已记录的参数,您可以告诉我我的JavaScript中是否存在问题吗?

首先运行runMe函数

 Ajax: function()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
                xmlhttp.setRequestHeader("Content-length", sVars.length);
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
},
tOrigin: function(origin){
    this.origin = origin;
},
tObject: function(origins,url,apik){
    this.origins=origins; //this is an array
    this.url=url;
    this.apik=apik; 
    this.host= "http://localhost:3000/";//window.location.hostname;
}
    runMe: function(){
                    var t = new tObject(['this','word','word me please','and me please','word','word','okay','word','go','go'],window.location.href,"helloapik");
    //  console.log(t);

        ajax = new Ajax();
        ajax.connect("http://localhost:3000/","POST",JSON.stringify(t), callBackFunc)
    }

这是我在rails服务器日志中获得的内容:

Parameters:

{"{\"origins\":"=>{"{\"origin\":\"this\"},{\"origin\":\"word\"},{\"origin\":\"word me please\"},{\"origin\":\"and me please\"},{\"origin\":\"word\"},{\"origin\":\"word\"},{\"origin\":\"word\"},{\"origin\":\"okay\"},{\"origin\":\"word\"},{\"origin\":\"go\"},{\"origin\":\"go\"}"=>{",\"url\":\"file:///Users/waheed/Desktop/untitled.html\",\"apik\":\"helloapik\",\"host\":\"http://localhost:3000/\"}"=>nil}}}

1 个答案:

答案 0 :(得分:1)

如果整个jQuery库的开销太大,你就可以把它拿去并选择你需要的东西。

在任何情况下,我都会说这是解析服务器端的问题,似乎有一些尾随的Ruby魔法正在进行,但是如果不知道你的服务器端代码,很难说出发生了什么。