Robert van der Spek
Robert van der Spek

Reputation: 1197

Network error in Jquery post

I am getting an error when doing a Jquery post (Error: A network error occurred.)

With Postman, it does work, here is the cURL:

POST /site/restapi/postalcode HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 14d39deb-fc28-3171-d688-18deaca7cbbe

{
"postalCode" : "9041AK",
"houseNumber" : "1"
}

Unfortunately with my javascript it does not:

$('input[name="houseNumber"]').blur(function() {
        // Check if the street and city can be fetched
        $.ajax({
            type : "POST",
            contentType : "application/json",
            url : "localhost:8080/site/restapi/postalcode",
            data: { postalCode : "9713GC", houseNumber : "1" },
            dataType: "json",
            crossDomain: true,
            success: function(data) {
                console.log(data);
            }}).fail(function(data, status, error) {
                console.log("error!" + error);
        });

    });

Edit: I have installed the firefox add-on Tamper Data, this gives insight to all requests being sent. This has given some additional information: no request is really being sent at all. (Apart from the GET request on loading the page.)

What is going on? Why does it not work?

Upvotes: 0

Views: 408

Answers (1)

t.niese
t.niese

Reputation: 40842

Your url in the request is wrong.

If you write "localhost:8080/site/restapi/postalcode", then localhost will - depending on the implementation of the url parser - referrer to protocol or the path will interpreted as relative path, but then localhost:8080 in both cases would not be interpreted as host.

You have to write one of the following:

  • "http://localhost:8080/site/restapi/postalcode"
  • "https://localhost:8080/site/restapi/postalcode"
  • "//localhost:8080/site/restapi/postalcode"
  • "/site/restapi/postalcode"

Which one depends on the url you initiate request from.

Upvotes: 1

Related Questions