如何修复jQuery AJAX的“不支持伪”错误?

时间:2022-01-08 08:58:19

I am trying to get response from a php page using jQuery Ajax. Everything works fine until I tried to explode an array and combined it elements to get a time 09:00.

我正在尝试使用jQuery Ajax从php页面获得响应。一切都很正常,直到我试图爆炸一个数组并将它的元素组合到09:00。

Console says, Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: 00 and nothing displayed.

控制台说,未捕获错误:语法错误,未识别表达式:不支持的伪:00,没有显示任何内容。

My code is,

我的代码,

$starttimeArr= explode(",",$comma_separated_starttime);// explodes 09,00,00
$endtimeArr= explode(",",$comma_separated_endtime);// explodes 17,00,00
echo $starttime= $starttimeArr[0].":".$starttimeArr[1];// combine to get 09:00. The line pop up the error
$endtime= $endtimeArr[0].":".$endtimeArr[1];// combines to get 17:00

How did I overcome this error? Any help will be appreciated.

我是如何克服这个错误的?如有任何帮助,我们将不胜感激。

My Ajax code is

我的Ajax代码是

jQuery("#_dob").change(function() {
                    jQuery.ajax({
                        url: "<?php echo $this->getUrl('deliverybydatepro/index/index') ?>",
                        data: "checkIn="+jQuery(this).val()+"&type=calendar",
                        type: "GET",
                        dataType: "html",
                        success: function(data) {
                           var $response=jQuery(data);
                           jQuery("#div1").html(data);
                        }
                    });
    });

The response page has a dropdown having an option "09:00". jQuery-1.8.0 triggers an error on it.

响应页面有一个下拉菜单,选项是“09:00”。jQuery-1.8.0触发一个错误。

3 个解决方案

#1


5  

Okay. Having seen the edited question with the JS code, the problem is clear:

好吧。看到用JS代码编辑的问题后,问题很明显:

var $response=jQuery(data);

This line is the cause of the error.

这条线是错误的原因。

The data variable is the response string from the AJAX request. This contains the string "09:00", as expected.

数据变量是来自AJAX请求的响应字符串。这包含了预期的字符串“09:00”。

This means that your code is equivalent of calling jQuery('09:00').

这意味着您的代码相当于调用jQuery('09:00')。

jQuery will try to interpret that as a CSS selector. It will see the 09 and try to find an element with that name. It won't find one of course, but it won't complain about that. However it will then see the :00 and assume that's a pseudo-selector (like :before or :first-child, etc). Of course :00 is not a valid pseudo-selector, and jQuery will complain about that. So that's where the error is coming from.

jQuery将尝试将其解释为CSS选择器。它将看到09并尝试找到一个具有该名称的元素。它当然找不到,但也不会抱怨。但是,它将看到:00并假设这是一个伪选择器(比如:before或:first-child,等等)。当然:00不是一个有效的伪选择器,jQuery会对此提出抱怨。这就是误差的来源。

So what to do about it? Well the answer is quite simple, really.

那么该怎么办呢?答案很简单,真的。

You're using this line to set a variable called $response, but then you're never using that variable; you're continuing to use the data variable. So really the whole of the line that is throwing the error is completely unnecessary. You may need a line like that if your PHP is outputting JSON or XML data, but not if it's a plain string.

你用这一行来设置一个变量叫$response,但是你永远不会用这个变量;继续使用数据变量。实际上,抛出误差的这条线是完全没有必要的。如果PHP输出的是JSON或XML数据,那么可能需要这样一行,但如果是纯字符串,则不需要这样一行。

So the solution is to remove that line entirely.

所以解决的办法就是把这条线完全去掉。

Hope that helps.

希望有帮助。

Just as an aside, to help you out for next time, it would have been fairly easy to find out which line of JS code was causing the problem by using the debugger in the browser. Just open the Dev Tools or Firebug, and run the code, and it would stop and show you exactly where the error is. A little bit of further work with the debugger looking at variables, and it would probably have become clear what the problem was.

顺便提一下,为了下次帮助您解决这个问题,在浏览器中使用调试器很容易找出导致问题的是哪一行JS代码。只要打开开发工具或Firebug,并运行代码,它就会停止并显示错误的确切位置。在调试器中再做一点关于变量的工作,很可能就会知道问题出在哪里了。

#2


1  

The php-answer is right, it sent the text '09:00' to javascript , but jQuery throws an error: "Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: 00", maybe ajax-answer is used as the determinant of element, maybe other . Show your js-code, because it throws an error

php-answer是对的,它将文本'09:00'发送给javascript,但是jQuery抛出了一个错误:“未捕获错误:语法错误,未识别表达式:unsupported pseudo: 00”,可能ajax-answer用作元素的行列式,也可能是其他。显示您的js代码,因为它会抛出一个错误

#3


1  

jQuery(data) was taken as the definition of an element with the pseudo-selector '09:00' and say that :00 - unsupported pseudo, becose there is the pseudo-selectors: first-child, hover, active and etc. In this strings:

jQuery(data)作为伪选择器'09:00'的元素定义,表示:00 - unsupported pseudo,因为存在伪选择器:first-child、hover、active等。

var $response=jQuery(data);
jQuery("#div1").html(data);
the string "var $response=jQuery(data);

is not needed, without this string script would work.

不需要,没有这个字符串脚本就可以工作。

#1


5  

Okay. Having seen the edited question with the JS code, the problem is clear:

好吧。看到用JS代码编辑的问题后,问题很明显:

var $response=jQuery(data);

This line is the cause of the error.

这条线是错误的原因。

The data variable is the response string from the AJAX request. This contains the string "09:00", as expected.

数据变量是来自AJAX请求的响应字符串。这包含了预期的字符串“09:00”。

This means that your code is equivalent of calling jQuery('09:00').

这意味着您的代码相当于调用jQuery('09:00')。

jQuery will try to interpret that as a CSS selector. It will see the 09 and try to find an element with that name. It won't find one of course, but it won't complain about that. However it will then see the :00 and assume that's a pseudo-selector (like :before or :first-child, etc). Of course :00 is not a valid pseudo-selector, and jQuery will complain about that. So that's where the error is coming from.

jQuery将尝试将其解释为CSS选择器。它将看到09并尝试找到一个具有该名称的元素。它当然找不到,但也不会抱怨。但是,它将看到:00并假设这是一个伪选择器(比如:before或:first-child,等等)。当然:00不是一个有效的伪选择器,jQuery会对此提出抱怨。这就是误差的来源。

So what to do about it? Well the answer is quite simple, really.

那么该怎么办呢?答案很简单,真的。

You're using this line to set a variable called $response, but then you're never using that variable; you're continuing to use the data variable. So really the whole of the line that is throwing the error is completely unnecessary. You may need a line like that if your PHP is outputting JSON or XML data, but not if it's a plain string.

你用这一行来设置一个变量叫$response,但是你永远不会用这个变量;继续使用数据变量。实际上,抛出误差的这条线是完全没有必要的。如果PHP输出的是JSON或XML数据,那么可能需要这样一行,但如果是纯字符串,则不需要这样一行。

So the solution is to remove that line entirely.

所以解决的办法就是把这条线完全去掉。

Hope that helps.

希望有帮助。

Just as an aside, to help you out for next time, it would have been fairly easy to find out which line of JS code was causing the problem by using the debugger in the browser. Just open the Dev Tools or Firebug, and run the code, and it would stop and show you exactly where the error is. A little bit of further work with the debugger looking at variables, and it would probably have become clear what the problem was.

顺便提一下,为了下次帮助您解决这个问题,在浏览器中使用调试器很容易找出导致问题的是哪一行JS代码。只要打开开发工具或Firebug,并运行代码,它就会停止并显示错误的确切位置。在调试器中再做一点关于变量的工作,很可能就会知道问题出在哪里了。

#2


1  

The php-answer is right, it sent the text '09:00' to javascript , but jQuery throws an error: "Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: 00", maybe ajax-answer is used as the determinant of element, maybe other . Show your js-code, because it throws an error

php-answer是对的,它将文本'09:00'发送给javascript,但是jQuery抛出了一个错误:“未捕获错误:语法错误,未识别表达式:unsupported pseudo: 00”,可能ajax-answer用作元素的行列式,也可能是其他。显示您的js代码,因为它会抛出一个错误

#3


1  

jQuery(data) was taken as the definition of an element with the pseudo-selector '09:00' and say that :00 - unsupported pseudo, becose there is the pseudo-selectors: first-child, hover, active and etc. In this strings:

jQuery(data)作为伪选择器'09:00'的元素定义,表示:00 - unsupported pseudo,因为存在伪选择器:first-child、hover、active等。

var $response=jQuery(data);
jQuery("#div1").html(data);
the string "var $response=jQuery(data);

is not needed, without this string script would work.

不需要,没有这个字符串脚本就可以工作。