2013-01-06 28 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