none
jquery的代码,在ie9中出了点问题 RRS feed

  • 问题

  • 我的网站是mvc3。用的是1.7.1的jquery。ajax请求代码如下:

        function ShowCity() {
            getCityURL = "/FindingYouManage/ShowCity?province=" + document.getElementById("province").value;
            $.ajax({
                scriptCharset: "utf-8",
                url: getCityURL,
                type:"POST",
                dataType: "json",
                data:"{}",          
                success: update_city
            });     
        }

        function update_city(result) {
            //$("#cityPanel").html(result);
            $("#cityPanel").empty();

            var html = "<select id=\"city\" name=\"city\">";
            $.each(result, function (entryIndex, entry) {
                html += "<option vlaue=\"" + entry + "\">" + entry + "</option>";
            });
            html += "</select>";

            $("#cityPanel").append(html);
           
        }

    经测试除了ie9外,chrome,firefox,opera,safari,均正常运行。

    在ie9中跟踪过ajax请求,请求正常发出,也有返回,当内容为空。下面是跟踪记录:

    <?xml version="1.0" encoding="UTF-8"?>
    <log>
     <version>1.1</version>
     <creator>
      <name>Internet Explorer 网络检查器</name>
      <version>9.0.8112.16421</version>
     </creator>
     <browser>
      <name>Internet Explorer</name>
      <version>9.0.8112.16421</version>
     </browser>
     <pages>
      <page>
       <startedDateTime>2012-02-02T09:16:28.318+08:00</startedDateTime>
       <id>0</id>
       <title/>
       <pageTimings>
        <onContentLoad>-1</onContentLoad>
        <onLoad>-1</onLoad>
       </pageTimings>
      </page>
     </pages>
     <entries>
      <entry>
       <pageref>0</pageref>
       <startedDateTime>2012-02-02T09:16:29.972+08:00</startedDateTime>
       <time>47</time>
       <request>
        <method>POST</method>
        <url>http://localhost/FindingYouManage/ShowCity?province=吉林省</url>
        <httpVersion>HTTP/1.1</httpVersion>
        <cookies>
         <cookie>
          <name>AdminCookieKey</name>
          <value>BF779E0933A882808585D19455CD7937</value>
         </cookie>
        </cookies>
        <headers>
         <header>
          <name>Accept</name>
          <value>application/json, text/javascript, */*; q=0.01</value>
         </header>
         <header>
          <name>Content-Type</name>
          <value>application/x-www-form-urlencoded</value>
         </header>
         <header>
          <name>X-Requested-With</name>
          <value>XMLHttpRequest</value>
         </header>
         <header>
          <name>Referer</name>
          <value>http://localhost/FindingYouManage/AddMessage</value>
         </header>
         <header>
          <name>Accept-Language</name>
          <value>zh-cn</value>
         </header>
         <header>
          <name>Accept-Encoding</name>
          <value>gzip, deflate</value>
         </header>
         <header>
          <name>User-Agent</name>
          <value>Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)</value>
         </header>
         <header>
          <name>Host</name>
          <value>localhost</value>
         </header>
         <header>
          <name>Content-Length</name>
          <value>2</value>
         </header>
         <header>
          <name>Connection</name>
          <value>Keep-Alive</value>
         </header>
         <header>
          <name>Cache-Control</name>
          <value>no-cache</value>
         </header>
         <header>
          <name>Cookie</name>
          <value>AdminCookieKey=BF779E0933A882808585D19455CD7937</value>
         </header>
        </headers>
        <queryString>
         <param>
          <name>province</name>
          <value>吉林省</value>
         </param>
        </queryString>
        <postData>
         <mimeType>application/x-www-form-urlencoded</mimeType>
         <text>{}</text>
        </postData>
        <headersSize>539</headersSize>
        <bodySize>2</bodySize>
       </request>
       <response>
        <status>200</status>
        <statusText>OK</statusText>
        <httpVersion>HTTP/1.1</httpVersion>
        <cookies/>
        <headers>
         <header>
          <name>Cache-Control</name>
          <value>private</value>
         </header>
         <header>
          <name>Content-Type</name>
          <value>application/json; charset=utf-8</value>
         </header>
         <header>
          <name>Server</name>
          <value>Microsoft-IIS/7.5</value>
         </header>
         <header>
          <name>X-AspNetMvc-Version</name>
          <value>3.0</value>
         </header>
         <header>
          <name>X-AspNet-Version</name>
          <value>4.0.30319</value>
         </header>
         <header>
          <name>X-Powered-By</name>
          <value>ASP.NET</value>
         </header>
         <header>
          <name>Date</name>
          <value>Thu, 02 Feb 2012 09:16:30 GMT</value>
         </header>
         <header>
          <name>Content-Length</name>
          <value>2</value>
         </header>
        </headers>
        <content>
         <size>2</size>
         <mimeType>application/json; charset=utf-8</mimeType>
         <text>[]</text>
        </content>
        <redirectionURL/>
        <headersSize>251</headersSize>
        <bodySize>2</bodySize>
       </response>
       <cache/>
       <timings>
        <send>0</send>
        <wait>31</wait>
        <receive>0</receive>
       </timings>
      </entry>
     </entries>
    </log>

     

    望高手不吝赐教,谢谢啦!

    2012年2月2日 9:18

答案

  •  getCityURL = "/FindingYouManage/ShowCity?province=" + document.getElementById("province").value;

    改成

     getCityURL = "/FindingYouManage/ShowCity?province=" + encodeURIComponent(document.getElementById("province").value);

     

    或是把$.ajax改成以下再試試看

        function ShowCity() {
            getCityURL = "/FindingYouManage/ShowCity";
            $.ajax({
                url: getCityURL,
                type:"POST",
                dataType: "json",
                data:{province:document.getElementById("province").value},          
                success: update_city
            });     
        }

     

     


    2012年2月3日 5:02