【问题标题】:$.ajax not working in chrome and firefox but is working in IE$.ajax 在 chrome 和 firefox 中不起作用,但在 IE 中起作用
【发布时间】:2014-11-04 06:01:41
【问题描述】:

我有以下代码。它在 IE 中运行良好,但在 Chrome 和 Firefox 中运行良好。没有显示错误。它只是没有得到输入的值。有人可以帮我解决问题。提前致谢。

File 'main.php'
---------------

<tr>
<td width="11%" class="style171"></td>
<td width="55%" bgcolor="#A8D3FF" class="style171"><strong>APPROVE</strong></td>
<td width="16%" bgcolor="#A8D3FF" align="center"><input type="radio" name="approve" id="approve" value="1" <?php if ($approve== '1') echo "checked"; ?> /></td>
<td width="18%"></td>
</tr>
<tr>
<td width="11%" class="style171"></td>
<td class="style171" bgcolor="#A8D3FF"><strong>NOT APPROVE</strong></td>
<td bgcolor="#A8D3FF" align="center"><input type="radio" name="approve" id="approve" value="2" onClick="processForm()" <?php if ($approve== '2') echo "checked"; ?> /></td>
<td width="18%"></td>
</tr>
<tr>
<td width="11%" class="style171"></td>
<td colspan="2" align="left"><div id="div_data"></div></td>
<td width="18%"></td>
</tr>


<script type="text/javascript">

function processForm() 
{   
    var val = 0;

    if(window.document.getElementById('approve').checked)
        var val = 1;

    $.ajax( {
        type: 'POST',
        url: 'not_approved.php',
        data: "value="+val,
        cache: false,
        success: function(html){
            $("#div_data").html(html);
        }
    } );
}

</script>


File 'not_approved.php'
-----------------------

<form id="formt" name="formt" method="post" action="">
  <table width="100%" border="0" align="left" cellpadding="1" cellspacing="0" bgcolor="#D8EEFE">
    <tbody>
      <tr>
        <td colspan="3"><table width="100%" border="1" bordercolor="#33CCFF" cellspacing="0" cellpadding="1">
          <tbody>
            <tr class="style2">
              <td align="left"><font color="#FF0000">*</font> Enter your comments here.
                <table width="430" border="0">
                  <tr class="style2">
                    <td width="10" align="left" valign="top"></td>
                    <td width="410" colspan="2" align="left" valign="top"><textarea name="comment" id="comment" cols="45" rows="5"><?php echo $comment; ?></textarea></td>          
                  </tr>
                </table></td>
            </tr>
          </tbody>
        </table></td>
      </tr>
    </tbody>
  </table>
</form>

【问题讨论】:

    标签: javascript ajax google-chrome firefox


    【解决方案1】:

    试试这个

                function processForm() 
                {   
                    var val = 0;
    
                    if(window.document.getElementById('approve').checked)
                        var val = 1;
    
                    $.ajax( {
                        type: 'POST',
                        url: 'not_approved.php',
                        data: {value:val},
                        cache: false,
                        success: function(html){
                            $("#div_data").html(html);
                        }
                    } );
                }
    
                </script>
    

    ** 数据字段发生变化**

    【讨论】:

    • 我试过了,还是不行。它显示“注意:未定义的索引:字段名”
    • 但是我在你的代码中看不到'fieldname'。所以请你提供完整的html代码
    【解决方案2】:

    您以错误的方式传递数据,如下所示

    data: "{'value':"+val+"}",
    

    如果值是字符串,那么

    data: "{'value':'"+val+"'}",
    

    data: JSON.stringify({value:val})
    

    【讨论】:

    • 我试过 'data: JSON.stringify({value:val})' 但它在 IE 中不起作用。另外两个适用于 IE,但不适用于 Chrome 和 Firefox。我收到此错误“通知:未定义索引:字段名”。
    【解决方案3】:

    你的数据变量不能是字符串格式,因为它是一个变量名。你应该这样做:

    <script type="text/javascript">
    
    function processForm() 
    {   
        var val = 0;
    
        if(window.document.getElementById('approve').checked)
            var val = 1;
    
        $.ajax( {
            type: 'POST',
            url: 'not_approved.php',
            data: {
                   value: val,
                  }
            cache: false,
            success: function(html){
                $("#div_data").html(html);
            }
        } );
    }
    
    </script>
    

    在这里,您的脚本变量 val 值分配给值变量,我们在这里使用冒号 ':' 而不是 AJAX 中的 =。在服务器端,捕获变量也必须具有相同的名称,即值。

    【讨论】:

    • 去掉cache:false这行再试试。
    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多