未捕获的TypeError:对象#<object>没有方法&#39;匹配&#39; </object>

时间:2013-12-11 02:27:53

标签: javascript jquery html dom

响应是一个html页面,标题元素中包含“错误”,并伴有无序的错误列表。

下面检查页面标题是否为“Error”,如果是,则抓取无序列表中的第一条错误消息,并将其传递给displayAjaxMessage函数。

它返回一个错误:Uncaught TypeError:Object#在以下行没有方法'match':

if (html.match(/<title>Error<\/title>/))
$("#profile-edit-form").validate({
    submitHandler: function () {
        $.ajax({
            url: $("#profile-edit-form").attr("action"),
            data: $("#profile-edit-form").serialize(),
            dataType: "html",
            type: 'POST',

            error: function (jqXHR, textStatus, errorThrown) {
                displayAjaxMessage("Sorry, there was an error logging in, please try again.");
            },
            success: function (html, textStatus, jqXHR) {
                if (html.match(/<title>Error<\/title>/)) {
                    var error = $(html).find('ul li:first').text();
                    if (error == "The password you submitted was not correct") {
                        var error_msg = "<p>The password is incorrect</p>";
                        displayAjaxMessage("The password you submitted was not correct", "#profile-edit-form-response");
                        $('#error-modal').modal('show');
                        $('.error-modal-message').show().html(error_msg);
                    } else if (error == "You must submit your username and password") {
                        displayAjaxMessage("You must submit your username and password.");
                    }
                } else {
                    $('#profile-updated-modal').modal('show');
                }
            }
        });
    }
});

enter image description here

HEADER:

Request URL:http://misn003.dev/en/
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:text/html, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,es;q=0.6
Connection:keep-alive
Content-Length:384
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:exp_perpage=50; PHPSESSID=ssrj2ld6ns51nhkbu3ndj1sre5; exp_last_visit=1386716108; exp_last_activity=1386726413; exp_expiration=1386733613; exp_anon=1; exp_stashid=a%3A2%3A%7Bs%3A2%3A%22id%22%3Bs%3A40%3A%2292de251be3920633cbb7b9988b1ff912b76d3396%22%3Bs%3A2%3A%22dt%22%3Bi%3A1386726421%3B%7D; exp_tracker=a%3A3%3A%7Bi%3A0%3Bs%3A12%3A%22profile%2Fedit%22%3Bi%3A1%3Bs%3A28%3A%22recipes%2Fview%2Fchicken-tostada%22%3Bi%3A2%3Bs%3A12%3A%22profile%2Fedit%22%3B%7D; exp_sessionid=58ebdf48291051d68fa40842a273d13f4be36f30
Host:misn003.dev
Origin:http://misn003.dev
Referer:http://misn003.dev/en/profile/edit
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
XID:f09c8a4d69001b422f6208a840452c130cf3f789
ACT:75
RET:http://misn003.dev/en/profile/edit
return:
params_id:KPfnEOQIeFiLcGIbulRdhGsGy
site_id:1
first_name:Chuck
last_name:Norris
username:chuck@gmail.com
street_address:123 Main St
city:Walnut Creek
state:Alaska
zip_code:90120
optin-checkbox:on
optin:Y
phone_number:555-555-5555
password:
password:pl
password_confirm:
Response Headersview source
Cache-Control:no-cache, must-revalidate
Connection:Keep-Alive
Content-Length:1638
Content-Type:text/html
Date:Wed, 11 Dec 2013 02:39:40 GMT
Expires:Sat, 26 Jul 1997 05:00:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.2.22 (Unix) DAV/2 PHP/5.4.17 mod_ssl/2.2.22 OpenSSL/0.9.8x
Set-Cookie:exp_sessionid=58ebdf48291051d68fa40842a273d13f4be36f30; expires=Wed, 11-Dec-2013 04:39:40 GMT; path=/
X-EEXID:e7ded3ff5760cd83c30d126f12723a915de3453c
X-Powered-By:PHP/5.4.17

2 个答案:

答案 0 :(得分:1)

不知何故,jQuery似乎正在解析您对对象的响应,而不是将响应作为字符串发送到您的成功处理程序。因此,请尝试在ajax请求中设置dataType: "html"选项。

答案 1 :(得分:0)

默认情况下,HTML正在转换为某个对象。

为什么不像对待列表那样处理标题标签?

        success: function (html, textStatus, jqXHR) {
            var nodes = $(html);
            if (html.find("title").text()==="Error") {
                var error = nodes.find('ul li:first').text();