2013-01-06 27 views
0

我正在与jquery/JS和PHP进行简单的客户机/服务器通信。它工作正常,直到数据中包含'.'jquery/js和php通信问题

试着用下面的标题:

  • asdf-wq1 --> works
  • test1 --> works
  • bigip1.local --> '.' is replaced with '_'

我已经添加了escape()功能,我的代码,但结果是一样的。

function xy(){ 
    for (var i = 0; i < nodes.length; i++) { 
     var xy = escape(nodes[i].title) +"=" +escape(nodes[i].translate.x + "/" + nodes[i].translate.y); 

     $.ajax({ 
      url: 'save_layout.php', 
      data: xy, 
      dataType: "text", 
      type: 'post', 
      success: function(output) { 
       $("#output").html(output); 
      }, 
      error: function (response, status, error) { 
       alert("error" + response.responseText); 
      } 
     }); 
    } 
} 

PHP:

foreach($_POST as $name=>$value) { 
    echo "$name $value \n"; 
} 

Firebug的输出请求:

 
POST http /frontend/save_layout.php 

200 OK 186ms 
jquery....min.js (Zeile 4) 
HeaderPostAntwortHTML 
Parameterapplication/x-www-form-urlencoded 
bigip1.local 470/390 

Quelle 
bigip1.local=470/390 

Firebug的输出(响应):

 
bigip1_local 470/390 

正如你所看到的 - 它似乎被送到到服务器正确,但在服务器上就像读到我们的$_POST - '.'是一个'_'突然。

希望有人能帮助我这里!

+0

尝试让jQuery进行编码:var xy = {}; xy [nodes [i] .title] = nodes [i] .translate.x +“/”+ nodes [i] .translate.y;'这样jQuery就会为您发布帖子正文。 – Chad

+0

嗨! THX为即时响应 - 但不幸的是我仍然面临同样的问题...我可以看到正确的价值“bigip1.local”在萤火虫发布数据,但在PHP中,我仍然得到“bigip1_local”只要我访问$ _POST ... – roegi

回答

3

你不应该手动转换data成字符串。 jQuery做到了。只需将一个对象而不是一个字符串传递给Ajax函数。

,你应该永远(永远!)使用escape()。这个功能被打破了,没有理由使用它。如果您因某种原因必须进行手动URL编码,请使用encodeURIComponent()

function xy(nodes) { 
    $.each(nodes, function (i, node) { 
     $.post("save_layout.php", { 
      title: node.title, 
      x: node.translate.x, 
      y: node.translate.y 
     }) 
     .done(function (output) { 
      $("#output").html(output); 
     }) 
     .fail(function (response, status, error) { 
      alert("error" + response.responseText); 
     }); 
    }); 
} 

另外请注意,我给你的代码做了一些其他的变化,使其在jQuery的背景下更地道:

  • nodes传递作为一个参数,而不是事实一个全局变量。这使得该功能更独立。
  • 使用$.each()来代替你的for循环。
  • 使用明确的$.post()而不是更通用的$.ajax()
  • 事实上,所有数据都以的值传递,而不是作为关键字。每个请求的密钥title,x,y将相同。这使服务器端更容易(客户端上的)。
  • 采用.done().fail()回调,可以因为他们的本性被连接到.post().get().ajax()是一个承诺的。您可以使用read more about $.Deferred and promises或者直接使用它 - 这是一种在jQuery中使用Ajax回调的非常方便的方法。

你可能要考虑的代码重构的东西,使一个 Ajax请求的所有对象,而不是为每个对象一个请求。 HTTP请求需要时间,最好将它们结合起来。

+0

+1如果这仍然不能解决问题,那么在PHP方面有一个问题。 – Chad

+0

它真的好像解决了它 - 我现在就测试并确认,然后接受解决方案!多谢!! – roegi

+0

thx很多!那是我正在寻找! – roegi

-1

在JavaScript代码,你可以尝试使用

JSON.stringify(values); 

然后就是json_decode()在你的PHP。

0
  1. 转义已弃用。更好地使用encodeURIComponent。
  2. 它的情况下,只需使用jQuery功能

    for (var i = 0, l = nodes.length; i < l; i++) { 
    
        var data = {}; 
        data[nodes[i].title] = nodes[i].translate.x + "/" + nodes[i].translate.y; 
    
        $.ajax({ 
         url: 'save_layout.php', 
         data: data, 
         dataType: "text", 
         type: 'post', 
         success: function(output) { 
          $("#output").html(output); 
         }, 
         error: function (response, status, error) { 
          alert("error" + response.responseText); 
         } 
        }); 
    
    } 
    
+0

嗨! thx为迅速反应!我喜欢你的接近,因为它更直接 - 但不幸的是,我仍然面临着同样的问题... 我添加了一个window.console.log([nodes [i] .title]);在ajax请求之前,它显示正确的值“bigip1.local”,但在PHP中,我仍然获得“bigip1_local”,只要我访问$ _POST ... – roegi