JQuery Ajax Xhr status 0

Topics: General, Writing modules
Dec 20, 2011 at 9:52 PM

Hi,

I have a simple view that uses Ajax (with jquery) to send an array of int to a controller. This is the code:

Ajax:

$("button").click(function () {
        var ff = { orderedIds: TeamList };
        $.ajax({
            type: 'POST',
            url: "http://localhost/fanta/rrr",
            data: JSON.stringify(ff),
            contentType: 'application/json, charset=utf-8',
            dataType: 'json',
           success: function (response) {
                    alert(response.name);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
        });
    });

and the controller

 [HttpPost]
        public JsonResult DoSomething(long[] orderedIds)
        {
            var data = new { name = "TestName" };
            return Json(data);
        }

with this route:

new RouteDescriptor {
                    Priority = 5,
                    Route = new Route(
                        "rrr",
                        new RouteValueDictionary {
                            {"area", "FantaNico"},
                            {"controller", "Team"},
                            {"action", "DoSomething"}
                        },
                        new RouteValueDictionary(),
                        new RouteValueDictionary {
                            {"area", "FantaNico"}
                        },
                        new MvcRouteHandler())
                }

Now the same view is shown both in public part and in admin part. If I click the button (to start the ajax transfer) in the public part everything works and I receive a success alert with the "TestName" ajax response. But, if I click the button in admin part, ajax throw me an error, xhr.status = 0, thrownError = "" and ajaxOptions = "error". The same code executed in the administration and in the public zone works differently.

I have verified,  entering debug, that despite the error in both parties, the controller is properly called and the raw call (show by fiddler) is the same.

The call:

POST http://localhost/fanta/rrr HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 17
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7
Content-Type: application/json, charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost/fanta/CreateTeam/41
Accept-Encoding: gzip,deflate,sdch
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: Orchrd-fanta-=%7B%22Exp-N42-Settings%22%3A%22open%22%2C%22Exp-N42-Navigation%22%3A%22open%22%2C%22Exp-N42-Featured-Items%22%3A%22closed%22%2C%22Exp-N42-Blog%22%3A%22closed%22%2C%22Exp-N42-New%22%3A%22open%22%2C%22Exp-N42-Fantacalcio%22%3A%22open%22%2C%22Exp-N42-FantaNico%22%3A%22open%22%7D; openid_provider=google; .ASPXANONYMOUS=UIA7bF3tzAEkAAAAZjQ4YmQ3NjEtNTI0ZS00ZTBlLTk2OTQtMGUwZmE4NzU0ZDYx0; ASP.NET_SessionId=tibjmkfh5u3vwdptllnsnyzz; __RequestVerificationToken_L2ZhbnRh=cB+todGLE7jykcYAwYxQWWOVJXmU0YtRzYShj+8uc4f0ClCALyy0jVwyJ8F4WgotKilIKYbL/8Qq0hh974Bqvl22QB6OyZsyiP4G7NTCRV5abMKDv05xneK7QsTIK02GgTArSbESo0AZlz/hApsjgaSOx+L/+6muNhK9DXJBxHo=; fbsr_153536844686290=xtHq923UmJDGubgzPRhYryA-h37DEGVqgaWYJVJITQM.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiIyLkFRQlJHcXl0QzlpY01YcHcuMzYwMC4xMzI0MzM5MjAwLjEtMTQ1Njk4NTk2M3x3TU11d0Nra2tRelJ3ZS1LY25QdWFWSnJMTjAiLCJpc3N1ZWRfYXQiOjEzMjQzMzQ0MjIsInVzZXJfaWQiOiIxNDU2OTg1OTYzIn0; .ASPXAUTH=78767576B35E86ED95DBFB54E18143A10E9F799FC178C4485B25B84F23BA28429C59DC264CE09194AB263F65C2E37875000E5DC8831DFDC034E1255798D1BEC61168A55905BC431B40A9B97F224A8D4636AB0B4FE4AA02748E1E0A32C53DA8B6411F64310071B99D53B3C18034E37052B19A5E875A84C179EFF4CDE585C619761D3315B04F74DF4E4D4F7F2BA44B1B39

{"orderedIds":[]}

The answers:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 20 Dec 2011 22:45:32 GMT
Content-Length: 19

{"name":"TestName"}

Lastly, I noticed, in debug mode, that the first error alert (the one with xhr.status = 0)  comes out before you enter into the controller.

I have disabled AntiForgery.

Is there anyone can halp me?

Thank you.

Coordinator
Dec 20, 2011 at 10:50 PM

Do you have the actual error that was returned by the server? When you say you disabled antiforgery, how?

Dec 21, 2011 at 5:18 AM

No, the actual error is not returned by the server because first comes out the alert with the error and then the controller is called.

In the Module.txt I setted "AntiForgery: disabled"

 

thanks

Coordinator
Dec 21, 2011 at 5:24 AM

You mean the error happens client side before it even calls into the server? Then that has nothing to do with Orchard.

Dec 21, 2011 at 6:57 AM
Edited Dec 21, 2011 at 7:01 AM

Yes, but the strange thing is that the same view works properly public side but  doesn't works admin side... 

And in Chrome and Firefox doesn't works but works properly in IE (both in public and admin side)..

It seems in admin side there is something that works differently..

Dec 21, 2011 at 6:58 PM

Ok, solved! :-)

 

I've used a <button>....</button> for trigger ajax call and the part edit in the admin side was not very happy!