2014-03-03 25 views
0

工作我只是有:response.split( 「」)[0]不是在IE

<script> 
    $('#blah').on('click', function(){ 
     $.ajax({ 
      type: 'POST', 
      url: 'ajax.php', 
      data: {"xxx":"yyy", "zzz":"nnn"}, // my variables... 
      success: function(response){ 
       // response = "success,123" from server side 
       // so for accessing to the values "success" and "123" I just: 
       status = response.split(",")[0]; 
       count = response.split(",")[1]; 
       // now "status" and "count" are undefined in IE while it's working in all other browsers 
      } 
     }); 
    }); 
</script> 

正如可以在上面的代碼中看到,服務器響應於Ajax是 「成功,123」 。

訪問到值「成功」和「123」我做的:

status = response.split(",")[0]; 
count = response.split(",")[1]; 

現在statuscountIE不確定的,而他們在每一個其他瀏覽器好。

我應該怎麼做才能解決這個問題? 謝謝

+0

日可能會有所幫助:http://stackoverflow.com/questions/20395060/javascript-split-not-working-in-ie9-lower –

+0

試試這個:response.toString()。split(「,」)[0]; –

+0

@ C-link無法正常工作 – behz4d

回答

0

儘管我不確定爲什麼IE會這樣做,但我現在還不能真正測試,更聰明的選擇是使用JSON進行響應。如果你不使用PHP只是搜索做到這一點的方式

// your response originally looks like this: 
// { status: 'success', count: 123 } 

// you can define $.ajax with { dataType : 'JSON' } instead of: 
response = $.parseJSON(response); 
console.log(response.status); 
console.log(response.count); 

:如果你使用PHP,json_encode可以用來把一個簡單的數組JSON:

<?php 
echo json_encode(array('status' => 'success', 'count' => 123)); 

然後你的語言。

這也將節省您的這裏麪包含以後的分離處理的反應頭痛(在他們,句子,例如)

+0

這是PHP,讓我看看這是否可以使它工作 – behz4d

+1

@ behz4d不要忘記解析JSON或在'$ .ajax'選項中將'dataType'定義爲'JSON'在使用之前,我已經更新了答案 – casraf

+0

這是有線的,不起作用! – behz4d

0

那是因爲你還沒有定義的variables.Instead:

status = response.split(",")[0];

我會嘗試

var status = response.split(",")[0];