AJAX 异步传输数据的问题

要异步传输的数据:

Xml代码  复制代码
  1. ....       
  2. <action xsi:type="basic:JavaScript"  script="index += 1;"/>  
  3. ....  

 Ajax异步传输代码:

Js代码  复制代码
  1. var postData = "input="+ escape(inputJSON) +"&script="+escape(xml)+  
  2.                    "&feedGeneral=" + escape(feedGeneral);  
  3. XmlHttpRequest.open("POST",url,true);  
  4. XmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
  5. XmlHttpRequest.send(postData);  

 postData在encode和unencode,最终导致在后台Servlet中得到得到数据+被空格代替,使得script中的index += 1;变成了index =  1;从而导致后台Java代码在跑script出现死循环。
在网上搜索,发现content-type使用application/x-www-form-urlencoded后:

Control names and values are escaped. Space characters are replaced by `+', and then reserved characters are escaped as 

described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by `%HH', a percent sign and two hexadecimal 

digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A'). 

 然而使用form来提交方式来发起request却不会出现类似的问题,而form默认的Content-Type也是application/x-www-form-urlencoded:

Js代码  复制代码
  1. $('test').innerHTML = "<form target='_blank' id='test_form' action='./gen_feed' method='post'>"  
  2.         + "<input type='text' name='input' /><input type='text' name='script' />"  
  3.         + "<input type='text' name='feedGeneral' /><input type='hidden' name='format' value='" + this.feed_type + "'   
  4.   
  5. />"  
  6.         + "<input type='submit' value='gen' /></form>";  
  7. var test_form = $('test_form');  
  8. test_form.elements[0].value = inputJSON;  
  9. test_form.elements[1].value = script;  
  10. test_form.elements[2].value = feedGeneral;  
  11. test_form.submit();  

 仍未发现问题到底出在何处,暂做备忘。暂时把script中的‘+’都用‘-’代替,index += 1;改成index -= -1;呵呵,以后有人看到这段自动生成的诡异脚本,不知道会作何感想,但现在也只能如此。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值