繁体   English   中英

AJAX发布请求(包含数组和其他对象的对象)无法正确解析为rails

[英]AJAX post request, an object that includes an array and other objects, can't be parsed correctly into rails

我想要的是获取一个适当的参数,如果您看到记录了该参数,能否告诉我问题是否出在我的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}}}

如果整个jQuery库的开销太大,则可以将其忽略并选择所需的内容。

无论如何,我会说这是您解析服务器端的方式的一个问题,似乎有一些落后的Ruby魔术,但是在不知道您的服务器端代码的情况下,很难知道发生了什么。

暂无
暂无

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

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