2013-05-01 78 views
0

我正在调查可以发送什么以及哪些不能通过jquery ajax发送,因为它给了我错误的结果。起初我认为这是与路径字符有关。但我发现它确实不是。这是一个jQuery的bug或什么?

因此,我写了一个Web服务,它记录了已发送的内容以及发送38个符号的jQuery AJAX代码。但每次,我的网络服务记录不同的符号,并以不同的顺序。我的代码如下。

HTML:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
</head> 
<body> 
    <div id="original"></div> 
    <div id="returnedsybmols"></div> 
    <div id="returnedunicode"></div> 
    <script src="Scripts/jquery-2.0.0.min.js"></script> 
    <script> 
     $(function() { 
      var symbols = "! \" # $ % & ' () * + , - ./: ; <=> ? [ \ ]^_ ` { | } ~ ! ¢ £ ? \\ | §"; 
      //var unicode ="サイトは有効なHTMLマークアップを使っていません。テキストを貼り付けてください。"; 

      var eachsymbol = symbols.split(' '); 
      //alert(eachsymbol.length); 
      for (var i = 0; i < eachsymbol.length; i++) { 

      } 


      eachsymbol.forEach(function (t) { 
       $.ajax({ 
        type: "POST", 
        url: "jsonstring.asmx/symbols", 
        data: "{'send':'" + t + "'}", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function (msg) { 
         var returnedText = msg.d; 
         $("#returnedsysmbols").append($("<p>").text(returnedText)); 
        }, 
        error: function (e) { 
         alert("error"); 
        } 
       }); 
      }); 

     }); 
    </script> 
</body> 
</html> 

ASP:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Net; 
using System.IO; 
using System.Text; 
using System.Xml; 
using System.Text.RegularExpressions; 
using System.Web.Script.Services; 

namespace ASP_NET 
{ 
    /// <summary> 
    /// Summary description for jsonstring 
    /// </summary> 
    /// 
    [System.Web.Script.Services.ScriptService] 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService] 
    public class jsonstring : System.Web.Services.WebService 
    { 

     [WebMethod] 
     public string symbols(string send) 
     { 
      using (StreamWriter sw = File.AppendText(@"E:\My Documents\programming\exercise\ASP_NET\ASP_NET\writeout\symbols4.txt")) 
      { 
       sw.WriteLine(send); 
      } 

      return send; 
     } 
     [WebMethod] 
     public string unicode(string send) 
     { 
      using (StreamWriter sw = new StreamWriter(@"E:\My Documents\programming\exercise\ASP_NET\ASP_NET\writeout\unicode.txt")) 
      { 
       sw.Write(send + " "); 
      } 

      return send; 
     } 
    } 
} 

显然\”,“无法通过jQuery的过去,我尝试使用转义功能,但它将编码我所有Unicode字符的任何。解决方案? 你能解释为什么我的网络服务器每次记录不同的数字和不同的字符集?

运行情况1 “()* +, - 。 /:; < =#! %&? > [`^ {! 〜? | £§

运行情况2 $()* +, - 。 /:; <#“%& => [] _`} |!?!?!{〜¢£§

运行情况3 $ &%()* - +/<>^_}〜¢ ?£§|

run case 4 $()* +, - 。/:; &“#<? ! > = _ [`| {}〜£| ! ? ¢

+0

你为每个符号发送一个ajax请求。 Ajax是异步的,所以你的第二个请求可以比第一个请求先完成。 不同的数字可能是因为请求尚未满足或完全不满意。使用fiddler检查http消息 – 2013-05-01 06:16:37

+0

尝试在服务器上使用客户端和javascriptserializer上的JSON.stringify。 – 2013-05-01 06:22:44

+0

用“a b c ... z”替换所有奇特的unicode,然后再试一次。也就是说,将问题简化为*最小*测试案例 - 显示序列错误*或* unicode编码错误。 – user2246674 2013-05-01 06:33:04

回答

2

$.ajax调用是异步的。无法保证在同时触发多个请求时,其中一个将先于另一个完成。

所以不,这不是一个错误。

+0

即便如此,我应该以不同的顺序得到相同的一组字符。但是角色的出现每次都是不同的。 – 2013-05-01 06:20:11

+0

正如其他人所建议的,使用提琴手或其他工具来嗅探导线,以查看实际的请求和响应。它有一个自然的解释。 – NilsH 2013-05-01 06:21:39

0

我刚刚意识到我的web服务不够快,无法处理所有的ajax查询。所以它有时不返回结果。