3

Actually i should display the JSON data in a browser, so far i am getting the data in browser response body but here is the problem. i am facing that not able to display it in browser here picture for better understanding, any one please help me. Thanks in advance.

HTML code is:

<!DOCTYPE html>
<html lang="en">
<head><title>Demo</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /></head>

<body>
  <input id="testbutton" type="button" value="Test" />
  <p id="results">results appended here: </p>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#testbutton").click(function() {
        $.ajax({
            url: 'http://localhost:8080/SMWS/Rest/parentService/parent/getSchoolDetails',
            dataType: 'jsonp',
            success: function(data) {
                $("#results").append('all good');
                alert(JSON.stringify(data));
            },
             error: function() {
                $("#results").append("error");
                alert('error');
            }
        });
    });
});
</script>
</body>
</html>

here is the image to understand

venkat
  • 111
  • 1
  • 7
  • are you getting alert – Afsar Dec 17 '15 at 06:47
  • no i am not getting the alert – venkat Dec 17 '15 at 06:47
  • did you tried `console.log(data[0]);` in success – Afsar Dec 17 '15 at 06:49
  • 1
    You're requesting jsonp and receiving json. Also that's the response body not the response header. – Jaromanda X Dec 17 '15 at 06:50
  • it is not even entering into success function always it is entering into failure function – venkat Dec 17 '15 at 06:50
  • 1
    As Jaronmanda said try keeping `dataType: 'json'` – Afsar Dec 17 '15 at 06:52
  • if i put its as json iam getting an error as some "Allow access control origin" ,if i placed jsonp i am getting the json data in response u can see it in the image above – venkat Dec 17 '15 at 06:54
  • The first parameter to error function would be something to log – Jaromanda X Dec 17 '15 at 06:54
  • sorry i didn't get you jaromanda – venkat Dec 17 '15 at 06:57
  • Jaromanda says that you should use `error: function(response) { console.log(response); }` – lexeme Dec 17 '15 at 06:58
  • 1
    this what i got in log Object { readyState: 4, getResponseHeader: .ajax/v.getResponseHeader(), getAllResponseHeaders: .ajax/v.getAllResponseHeaders(), setRequestHeader: .ajax/v.setRequestHeader(), overrideMimeType: .ajax/v.overrideMimeType(), statusCode: .ajax/v.statusCode(), abort: .ajax/v.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 10 more… } – venkat Dec 17 '15 at 07:03

1 Answers1

1
  1. There are plenty of questions about ajax requests that have response status 200 but raise errors. Take a look at this one for example. In this thread it is suggested that error might be caused by the malformed json the server is sending to you. You can check out Corvin's answer and jaketrent's answer.
  2. In order to find out the error response message you may look at this thread. Define this as your error handler and look for the responseText property of xhr parameter:

    error: function(xhr, status, error) {
        console.log(xhr.responseText);
    }
    
Community
  • 1
  • 1
lexeme
  • 2,915
  • 10
  • 60
  • 125
  • 1
    when i replace with that in console showing it as "undefined" – venkat Dec 17 '15 at 07:33
  • checkout if the server really supports the **jsonp** requests: is there `"Access-Control-Allow-Origin": "http://localhost:8080"` in your response header? – lexeme Dec 17 '15 at 07:43