Pawan
Pawan

Reputation: 32331

Jquery webservice call Cannot read property 'contentDocument' of undefined

I am sending this valid json response from the backend

[
    {
        "id": 123,
        "vendorName": "PoppyCounter",
        "item": "Chocltae"
    },
    {
        "id": 1234,
        "vendorName": "PoppyCounter",
        "item": "Chocltae"
    },
    {
        "id": 12345,
        "vendorName": "PoppyCounter",
        "item": "Chocltae"
    }
]

I am making a webservice call from Jquery as shown below

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript">

    $(document).ready(function() {
        $.ajax({
            type: 'GET',
            url: 'http://192.168.2.46:8086/Poller/poll/initial',
            jsonpCallback: 'jsonCallback',
            dataType: 'jsonp',
            success: function (msg) {

            },
            error: function (e) {
                $("#divResult").html("WebSerivce unreachable");
            }
        });
    });



    </script>
</head>
<body>
    <div id="divResult" style="margin-top: 20px;">
    </div>
</body>
</html>

I am getting the following exception

TypeError {stack: (...), message: "Cannot read property 'contentDocument' of undefined"}

Could anybody please tell me how to resolve this error ??

Upvotes: 0

Views: 233

Answers (1)

Blake Simpson
Blake Simpson

Reputation: 1088

You are asking for JSONP but the response from your backend is raw JSON, the response should be more like:

jsonCallback([
  {
    "id": 123,
    "vendorName": "PoppyCounter",
    "item": "Chocltae"
  }
])

There should also be a global function called jsonCallback that will get the data array as an argument.

function jsonCallback (data) {
  console.log(data);
}

You have to setup the backend to format the JSONP, you can find the correct callback name as jQuery will send "jsonCallback" as the GET parameter "callback".

The backend should then serve the request as application/javascript instead of application/json.

Upvotes: 1

Related Questions