2011-03-22 81 views
1

我的代码如下所示。通过Ajax发送包含数据的“@”符号

$.ajax({    
    url:'http:://www.sample.com/checkmail/'+$('#txtemail').val(), 
    success: function(data) 
    { 
     $('#response').html(data); 
     if(data!="Success")     
     { 
      $('#txtemail').css("background-color","#FF8A8D"); 
     } 
     else 
     { 
      $('#txtemail').css("background-color","white"); 
     } 
    } 
}); 

上面代码中传递数据时,没有工作的 '@' 符号

例子:

//passing Hello 
txtemail = "Hello" 

//ajax response message in firefox 
GET http://www.example.com/checkmail/Hello 200 OK 503ms 

但是,如果我通过电子邮件就像下面提示错误

Example: 
//passing hello 
txtemail = "[email protected]" 

//ajax response message in firefox 
GET http://www.example.com/checkmail/[email protected] 400 Bad Request 26ms 

任何建议如何解决这个问题

+0

您的网址中有错字:通过'http :: //' – Unicron 2011-03-22 07:08:40

回答

3

使用电子邮件地址上的encodeURIComponent()

url:'http://www.sample.com/checkmail/'+encodeURIComponent($('#txtemail').val()), 

@处于URL(对于username:[email protected]方案)保留字符并需要%的编码。

+0

谢谢。发送电子邮件时,我用%40替换了@符号。再次终结点我不得不再次(%40到@)。我做了很长的路。感谢您使用简单的方法。 – llcoollasa 2011-03-24 04:24:47

0

您必须编码URL作为宇宙大帝说ü也可以使用这个escape()函数....

url:'http://www.sample.com/checkmail/'+escape($('#txtemail').val()) 
+3

不要使用'escape()'。请参阅:http://stackoverflow.com/questions/75980/best-practice-escape-or-encodeuri-encodeuricomponent – Tomalak 2011-03-22 07:30:32

+0

谢谢Tomalak ............ – 2011-03-22 09:41:41