开发者

jquery ajax call return 200, valid json, fires error event

开发者 https://www.devze.com 2023-03-22 09:23 出处:网络
I have read many other posts on here that state the same issue and it turns out to be invalid json. This however is not my problem, I am surrering from the same symptoms but the json is valid as far

I have read many other posts on here that state the same issue and it turns out to be invalid json.

This however is not my problem, I am surrering from the same symptoms but the json is valid as far as I can tell:

ok, this is my code:

function CheckEmail(email) {
    $.ajax({
        type: "GET",
        url: "<%=_baseUrl%>/CheckEmail.ashx?email=" + email,
        dataType: "json",
        success: function (response) {
            var json = response;

            if (json.emailExists == "true") {
                emailValid = false;
                $('#emailError').html('Email address already exists in our system, please try a different address');
            }
            else {
                emailValid = true;
                $('#emailError')开发者_运维技巧.html('');
            }
        },
        error: function (response, ajaxOptions, thrownError) {
            emailValid = false;
        }

    });
}

I am always getting into the error event.

This is the response from the ashx handler (as seen through fiddler):

Headers

HTTP/1.1 200 OK
Date: Fri, 22 Jul 2011 08:22:39 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 23

Content

{"emailExists":"false"}

Can someone suggest why this might be failing??

thrownError = "" ajaxOptions = "error" and the errorText in the response = "error"


Could this - jquery ajax has a problem getting return value from ashx handler - be a possible answer? Seems to be having the same problem as you!

EDIT: I'm curious about the reasons behind it as well. A null might be returned if it's an asynchronous ajax call (http://roshanbh.com.np/2008/01/how-to-return-value-from-ajax-function-use-synchronous-request.html), which jQuery ajax is by default. But yours works fine if it's a relative path. Maybe this is a problem where the absolute path causes a cross-domain call, and the return value is ignored for some reason? Are you able to try using an absolute path and force synchronous ajax call?


I don't know why you are getting the error, but I noticed two things in your code you should change (but I don't think they are the cause of the error).

This:

url: "<%=_baseUrl%>/CheckEmail.ashx?email=" + email,

Should be:

url: "<%=_baseUrl%>/CheckEmail.ashx",
data: { email: email }

And this:

{"emailExists":"false"}

Is probably better as:

{"emailExists":false}

So you get an actual false value in javascript, and not a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号