Jeremy Barnes
Jeremy Barnes

Reputation: 677

AJAX request fails in Firefox, works in IE (server returns 200 in both cases)

I am testing locally. I have IIS serving JS and HTML on localhost:50972 and Java/Jersey acting as an application server on localhost:8080.

The following AJAX request succeeds in Internet Explorer, but fails in Chrome and Firefox, even though the server shows 200 OK:

public getTest() {
    var settings: JQueryAjaxSettings = {
        url: "http://localhost:8080/getData",
        type: "GET",
        crossDomain: true,
        dataType: "text",
    };

    jQuery.ajax(settings).done(function (o) {
        alert(o);
    }).fail(function (request) {
        alert(request);
    });
}

The code on the Java side looks like this:

@GET
@Path("/getData")
public Response getData() {

    NewCookie cookie = new NewCookie("test", "key:val", "/", null, "comment", 100, false );
    return Response.status(Response.Status.OK).entity("Hello World").cookie(cookie).build();
}

Below are the relevant HTTP Requests/Responses from IE and Firefox:

Internet Explorer Request (Succeeds)

GET http://localhost:8080/getData?_=1451863561652 HTTP/1.1
Referer: http://localhost:50972/
Accept: text/plain, */*; q=0.01
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Connection: Keep-Alive
DNT: 1
Host: localhost:8080

Firefox Request (Fails)

GET http://localhost:8080/getData?_=1451863686206 HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
Accept: text/plain, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:50972/
Origin: http://localhost:50972
Connection: keep-alive

Response from server (sent to both)

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: test=key:val;Version=1;Comment=comment;Path=/
Content-Type: text/plain
Content-Length: 11
Date: Sun, 03 Jan 2016 23:26:07 GMT

Hello World

I have also tried this with the response as {} and dataType: json instead of Hello World and dataType: text, but with no change. I have also tried with crossDomain: true and with crossDomain: false Help?

Upvotes: 3

Views: 543

Answers (1)

user2864740
user2864740

Reputation: 61875

The observed behavior is because Firefox (and Chrome, etc.) correctly consider a different port to establish a different origin; IE does not.

See MDN - Same Origin Policy:

Two pages have the same origin if the protocol, port (if one is specified), and host are the same for both pages..

..[but] IE doesn't include port into Same Origin components, therefore http://company.com:81/index.html and http://company.com/index.html are considered from same origin and no restrictions are applied.

Use CORS - enabled on the server - for the request to succeed in all browsers.

Upvotes: 2

Related Questions