1

I am new to Ajax and trying to fill a table with the data coming from a server.

Here is my GET function:

<script type = "text/javascript" charset="utf-8">
    $(document).ready(function () {
        $.ajax({
            type: "GET",
            url: "myURL",
            dataType: "jsonp",
            success: function (response) {
                debugger;
                var trHTML = '';
                $.each(response, function (i, v) {
                    // append to table
                    trHTML += '<tr><td>' + v.Ticker+
                              '</td><td>' + v.Close+
                              '</td><td>' + v.percentage+
                              '</td></tr>';
                });
                $("#art").append(trHTML);
            },
            error: function (e) {
                debugger;
                console.log(e)
                alert(e + " Error");
            }
        });
    });
</script>

Incoming data falls into Error every single time and it is like:

e = Object {readyState: 4, responseText: "{"Ticker":"akbnk.is","Close":6.74,"percentage":-1…s","Close":1.54,"percentage":9.2198581560283781}]", s

I can see this much on Chrome. Does anybody know how to fix this?

kalahari
  • 895
  • 5
  • 15
  • 34
  • 1
    As i can see your response not seems like Valid json. So remove `dataType: "jsonp"` – Manwal Oct 06 '15 at 12:28
  • Oh, thanks a lot. Can you write this as an answer? – kalahari Oct 06 '15 at 12:30
  • @kalahari jsonp is needed when your ajax call is communicating with a different domain. If this isn't the case you can replace jsonp without any risk. – nowhere Oct 06 '15 at 12:39
  • JSONP was superseded by CORS years ago. There's no good reason to do new development using JSONP in 2015. – Quentin Oct 06 '15 at 12:45

2 Answers2

0

You said:

dataType: "jsonp",

Your data says:

{"Ticker":"akbnk.is","C

Your data appears to be JSON. JSON is not JSONP, so it errors.

Either say "json" or change the server to return JSONP.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Your response not JSONP so when you trying to use ajax request with dataType: "jsonp", response must be of same JSONP. If it will not then it doesn't come in success callback.

So either remove dataType: "jsonp" or change dataType: "json" or change your response to JSONP.

Community
  • 1
  • 1
Manwal
  • 23,450
  • 12
  • 63
  • 93