开发者

jQuery POST AJAX => No Response Header

I have a problem with a jQuery AJAX call. Sometimes it works and sometimes I don't get any response from the server. The passed parameters are correct, but no callback function will be invoked except for beforeSend. I don't even get a HTTP Response Header from the server. The request header seems to be correct and looks exactly like one of the correct calls. Any Suggestions?

[UPDATE] Ok, here is a code snippet and a request header, but i don't think, that this is a implementation problem... FYI: I've deleted the cookie values in the request header

jQuery.ajax
        (
            {
                type: 'POST',
                url: 'index.php?eID=doSomeStuff',
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                dataType: 'json',
                data:
                {
                    obj_php_config: obj_php_config,
                    obj_php_search: obj_php_search,
                    selectedBox: selectedBox
                },
                error:
                    function (obj, textStatus, errorThrown)
                    {
                        alert("status=" + textStatus + ",error=" + errorThrown);
                    },
                success:
                    function (data, textStatus)
                    {
                         doSomeStuffWithData(data);
                       }

            }
        );
Host              myDomain.loc
User-Agent        Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept  开发者_如何学编程          application/json, text/javascript, */*
Accept-Language   en-us,en;q=0.5
Accept-Encoding   gzip, deflate
Accept-Charset    ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive        115
Connection        keep-alive
Content-Type      application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With  XMLHttpRequest
Referer           http://myDomain.loc/aSite.html?page=1
Content-Length    567
Cookie            --redacted by user--


I realise this post is pretty old now but wanted to post to help others. I just resolved a similar (if not the same) issue.

The problem in my case was that the same click event that triggered the AJAX post (via jquery event binding) was redirecting the page using href. Therefore the post didn't actually fail but was interrupted, I assume this is why there is no response info.

In short - check to make sure a redirect is not being triggered anywhere.

Depending on the timing of the page redirection, the call might work correctly from time to time. I guess?

Anyways, hope this helps someone.


be sure that your server responses this way:

header('Content-type: application/json');
echo json_encode(array("test" => "test"));
exit;

second guess, regarding the POST error, maybe invalid obj keys in your data obj, add quotes:

{
"obj_php_config": obj_php_config,
"obj_php_search": obj_php_search,
selectedBox: selectedBox
},


Update

You know -- I actually had a very similar problem come to think of it.. and I still to this day am not sure exactly why..

I replaced the $.ajax call with

$.post( 'url being called',
       { key:value},
       function (response) {
           // handle success/error response
       }
);

I really don't know how this makes any difference though.


Does firebug give you any error status? the NET information tab is extremely useful when working with Ajax.

Some code would be nice

EDIT: If you're doing any cross domain posts you'll need to using json with padding


Just to add on this, the problem I had was my url was being redirected or it was CANCELLED by one of my chrome extension, Adblock... the url was matching */post_save_advert/

It worked after disabling chrome extension for the specific site. I then also renamed it from save_advert to another url so it wont be blocked again.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜