php - jquery $.getJSON 仅在 Internet Explorer 中有效一次

标签 php javascript jquery ajax json

我有一个 php 函数,可以将搜索栏插入到网站的每个页面中。

该网站会检查用户是否启用了 javascript,如果启用了,它会插入一些 jquery ajax 内容来链接选择框(而不是使用它的后备 onchange="form.submit()")。

$.getJSON 在除 IE 之外的其他浏览器中对我来说非常适合,如果我在 IE 中进行全页刷新(ctrl+F5),我的 ajax 可以完美地工作,直到我导航到新页面(或与 $PHP_SELF 相同的页面) )通过提交表单或单击链接,jquery onchange 函数会触发,但随后 jquery 会抛出错误:

网页错误详细信息

Message: Object doesn't support this property or method

Line: 123

Char: 183

Code: 0

URI: http://~#UNABLE~TO~DISCLOSE#~/jquery-1.4.2.min.js

jquery 函数 $.getJSON() 好像不见了???

这似乎是某种缓存问题,因为它发生在第二页加载时,但我认为无论如何我已经采取了所有缓存预防措施,这是广告 jquery 功能的代码片段:

if (isset($_SESSION['NO_SCRIPT']) == true && $_SESSION['NO_SCRIPT'] == false)
        {
            $html .= '<script type="text/javascript" charset="utf-8">';
            $html .= '$.ajaxSetup({ cache: false });';
            $html .= '$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
                        alert(textStatus);
                        alert(errorThrown);
                        alert(XMLHttpRequest.responseText);
                        }});';
            $html .= '</script>';

            $html .= '<script type="text/javascript" charset="utf-8">';
            $html .= '$(function(){  $("select#searchtype").change(function() { ';
            $html .= 'alert("change fired!"); ';
            $html .= '$.getJSON("ajaxgetcategories.php", {id: $(this).val()}, function(j) { ';
            $html .= 'alert("ajax returned!"); ';
            $html .= 'var options = \'\'; ';
            $html .= 'options += \'<option value="0" >--\' + j[0].all + \'--</option>\'; ';
            $html .= 'for (var i = 0; i < j.length; i++) { options += \'<option value="\' + j[i].id + \'">\' + j[i].name + \'</option>\'; } ';
            $html .= '$("select#searchcategory").html(options); }) }) }) ';
            $html .= '</script> ';

            $html .= '<script type="text/javascript" charset="utf-8"> ';
            $html .= '$(function(){  $("select#searchregion").change(function() { ';
            $html .= 'alert("change fired!"); ';
            $html .= '$.getJSON("ajaxgetcountries.php", {id: $(this).val()}, function(j) { ';
            $html .= 'alert("ajax returned!"); ';
            $html .= 'var options = \'\'; ';
            $html .= 'options += \'<option value="0" >--\' + j[0].all + \'--</option>\'; ';
            $html .= 'for (var i = 0; i < j.length; i++) { options += \'<option value="\' + j[i].id + \'">\' + j[i].name + \'</option>\'; } ';
            $html .= '$("select#searchcountry").html(options); }) }) }) ';
            $html .= '</script> ';
        }; return $html;

记住,这是 php 函数的一部分,它将脚本插入到 html 中,抱歉,如果它看起来有点乱,我是 PHP 和 Javascript 新手,而且我也有点不整洁:)

还请记住,第一次访问时这在 IE 中工作得很好,但在任何导航后我都会收到错误。

最佳答案

好的,我找到答案了。问题出在IE和每个网页的标签中的这行代码:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

我的标签在该行之前声明了 jquery,它需要在该行之后。

这一定是 IE 中的一个错误。它会导致一个奇怪的结果,你不觉得吗?

关于php - jquery $.getJSON 仅在 Internet Explorer 中有效一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2863996/

相关文章:

php - in_array() 验证不起作用

php - SEO 友好的 .htaccess RuleRewrites

javascript - 错误 : Webpack 2 can't resolve babel-loader

javascript - 想更新我的 `ng-model' 值 Angular

javascript - Vite: resolve.alias - 如何解析路径?

jquery - 带有编辑操作子菜单的导航菜单

JQuery 验证未捕获类型错误 : Cannot read property 'settings' of undefined

javascript - 使用 .text() 选择一个类,但在 html 中存在空格问题

php - 使用从 2 个数据库中选择,然后将结果放入一个 php do 函数中

javascript - 我应该如何修改 PHP 和 Javascript 文件,以便复选框是强制性的?