2

I am trying to call following url using get request.

http://localhost:8080/abc/employees

When I opened above url in browser, I am getting following response.

[{"firstName":"Hari Krishna","id":1,"lastName":"Gurram","permAddrees":{"area":"Marthali","city":"Bangalore","country":"India","state":"Karnataka"},"tempAddrees":{"area":"Electronic City","city":"Bangalore","country":"India","state":"Karnataka"}},{"firstName":"PTR","id":2,"lastName":"PTR","permAddrees":{"area":"Bharath Nagar","city":"Hyderabad","country":"India","state":"Andhra Pradesh"},"tempAddrees":{"area":"BTM Layout","city":"Bangalore","country":"India","state":"Karnataka"}}]

Following is my jquery snippet.

<!DOCTYPE html>
<html>
    <head>
        <script src = "jquery-3.1.0.js"></script>
    </head>

    <body>  
        <h1>Click the below button to see the AJAX response</h1>
        <input type="submit" value="click here" id="button1" />

        <div id="placeHolder">

        </div>


        <script type="text/javascript">
            var jq = jQuery.noConflict();

            jq(document).ready(function(){
                jq("#button1").click(function(){
                    alert("Hello");
                    jq.get("http://localhost:8080/abc/employees", function(data, status){
                        alert(data + "" + status);
                        jq("#placeHolder").html(response);
                    });
                });
            });
        </script>
    </body>
</html>

When I click the button, it is showing first alert box 'alert("Hello");', but the alert box in call back function is not executed. Can any one help me to figure out the issue with my code. enter image description here

enter image description here

1
  • You have response in your call, but you're not defining it anywhere - you should be getting an error in console. You're actually making call to /abc/employees/1 in the screenshot. By any chance is your output different based upon what header the request uses (json / html)? Also - please not use alert but console.log - alert by default stringifies everything, so it's useless in debugging (you still should have a string in Chrome Response tab irregardless) Commented Aug 17, 2016 at 16:50

2 Answers 2

1

Short answer

Safari allows it and chrome does not allow (CROSS ORIGIN REQUEST SHARING - CORS).

An experiment :

Let's execute a request from this html file to "www.google.com" from our "localhost".

What happens in chrome ?

In chrome , CORS (CROSS ORIGIN REQUEST SHARING) is not allowed and hence it stops you in doing that.

Chrome gives the error as show below for http://www.google.com

Read about it here...

What happens in Safari ?

Safari allows this behaviour and hence you get this response from http://www.google.com

Proof I have added url as http://www.google.com, and hence this response comes

Sign up to request clarification or add additional context in comments.

Comments

0

Looks like you want to access localhost for your file which is not part of locahost

Two things can be done:

  1. Make the file a part of your localhost web application

  2. Make proper CORS settings, for instance is you are using JERSEY use the following.

How to handle CORS using JAX-RS with Jersey

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.