0

I use Spring for running my rest API service, i cant get the list of json object that my service send from a sample html file and plz tell me how can i access to the first object.

this is the sample output of my rest API service:

[{"src_ip":"1.1.1.1","src_id":"98","date":1470527874000},
{"src_ip":"1.1.2.1","src_id":"25","date":1470527934000},
{"src_ip":"1.1.2.1","src_id":"25","date":1470527934000}]

and this the code that i used in my html file:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing Results</title>
<!--TODO badan version e CDN e jquery use shavad-->
<script type="text/javascript"
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $.ajax({
        url: "http://127.0.0.1:8080/restapi2",
        dataType: "jsonp"
    }).then(function(data) {
       $('.List').append(data);
       $('.data').append(data[0]);
    });
});
</script>
</head>
<body>
    <div>
        <br><br>
        <p class="List"></p>
        <br><br>
        <p class="data"></p>
    </div>
</body>
</html>

I should say that when i run the sample of this link on my html file, it worked Properly.

Updated part:

after fixing last error,still I didn't get any correct data to show in my browser, but this time, the console get something but i don't know how to use them. this is a snapshot of it and the left side show that two object were sent.

enter image description here

and this content of that object:

enter image description here

Lord ST
  • 513
  • 7
  • 15
  • Is your Spring application returns the data ? Are you getting data when you just enter the url in browser/ or any other tools like POSTMAN ? – Tharsan Sivakumar Aug 09 '16 at 10:53
  • i don't try it on postman, but when i checkout "http://localhost:8080/restapi2" i see my response. – Lord ST Aug 09 '16 at 10:54
  • postman getted response too. – Lord ST Aug 09 '16 at 10:55
  • Then just open the developer console of your browser and put a break point at $('.List').append(data); and check the result – Tharsan Sivakumar Aug 09 '16 at 10:57
  • there was a err telling this: **getting my rest.html:1 XMLHttpRequest cannot load http://127.0.0.1:8080/restapi2. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.** – Lord ST Aug 09 '16 at 11:04
  • Are you running your javascript in the same machine or different machine ? – Tharsan Sivakumar Aug 09 '16 at 11:05
  • both are same, as i mentioned i run a spring web service for creating rest response, then i created a html file in my desktop for getting that response, although when i check, the html files that i created in my project space in intellij, have the same problem. – Lord ST Aug 09 '16 at 11:10
  • What's the url that you are using to host this html/javascript ? (i.e How are you accessing this page from browser)? Something like http://localhost:8080/someapp/ ? – Tharsan Sivakumar Aug 09 '16 at 11:14
  • yes, I use http://localhost:8080/restapi2 – Lord ST Aug 09 '16 at 11:17
  • Is it resolved now ? – Tharsan Sivakumar Aug 09 '16 at 11:40
  • not yet, i add **@CrossOrigin(origins = "http://localhost:8080")** in my appContoller.java for allowing CORs in server side but i still couldn't find a way for make it work on client *.html side ... – Lord ST Aug 09 '16 at 11:47

1 Answers1

0

Based on the discussion on the comment the reason is you were trying to access the different domain than your page which is prohibited by browsers as a security precaution.

$(document).ready(function() {
    $.ajax({
        url: "http://localhost:8080/restapi2"
    }).then(function(data) {
       $('.List').append(data);
       $('.data').append(data[0].);
    });
});

"No 'Access-Control-Allow-Origin' header is present on the requested resource"

Community
  • 1
  • 1
Tharsan Sivakumar
  • 6,351
  • 3
  • 19
  • 28
  • I update my code, for getting rid of "No 'Access-Control-Allow-Origin' header" but again i didn`t get any output. – Lord ST Aug 09 '16 at 12:27
  • can you help me with this part again, I really cant figure out what's the reason of this problem? – Lord ST Aug 12 '16 at 19:49
  • If both(your html page and your service) are in same domain you don't need to do any configuration for 'No Access Control origin'. Make sure both are correct, there may be typos as well – Tharsan Sivakumar Aug 13 '16 at 16:17