0

I'm new to coding...just wanted to know what am i doing wrong in this

<script type="text/javascript">

    // This example loads the "Canadian Parliament 2012"
    // dataset from a CSV instead of from JSON.        // System.import('app').catch(function (err) { console.error(err); });
    $(function()  {
        Dimensions = "sector_type";
        Measures = "";
        tblname = "sample";
        $.ajax({
            method: "GET",
            url: "http://localhost:5000/api/values/5"
            headers: {
                'Content-Type': 'application/json'
            },
            traditional: true,
            async: false,

        }).success(function results(data) {
            chartdata = data.data;
            alert("SUCCESS");
        });





    });

</script>

This is giving me Uncaught Reference :$ is not defined.Also is this the right way of writing a small script for retrieving data from localhost:5000 and what should i do to display the data Any Help would be deeply appreciated

Sebastian Simon
  • 14,320
  • 6
  • 42
  • 61
Venky
  • 1,573
  • 2
  • 18
  • 29
  • 7
    did you include jquery script? – guradio Dec 15 '16 at 05:18
  • Check Jquery plugin is referenced to your page – Bharat Dec 15 '16 at 05:18
  • your var $ is not defined. you need to include query script for defining $ var. – Ajay Thakur Dec 15 '16 at 05:22
  • 1
    `Also is this the right way of writing a small script for retrieving data from localhost:5000` - if it's a different host and/or port, does the server issue CORS headers? As for `async: false` - you'll get deprecation warnings about synchronous network requests - one day, it may just stop working altogether – Jaromanda X Dec 15 '16 at 05:27
  • can you confirm that you have included the JQuery library in your page? – blurfus Dec 15 '16 at 05:33
  • 1
    Possible duplicate of [Uncaught ReferenceError: $ is not defined?](http://stackoverflow.com/questions/2075337/uncaught-referenceerror-is-not-defined) – SDK Dec 15 '16 at 05:44
  • @ochi yea..i've included JQuery library – Venky Dec 15 '16 at 09:11
  • @Jaromanda X what do you mean ?? – Venky Dec 15 '16 at 09:11
  • @Venky - you seem to have discovered the synchronous request warning 3 hours ago ... as far as CORS goes, wait and see – Jaromanda X Dec 15 '16 at 09:49
  • @JaromandaX Yea synchronous thing is now ok now, thanks a lot for your help though,but for some reason it says connection refused – Venky Dec 15 '16 at 10:17

5 Answers5

1

It's worked for me. You should put the reference jQuery link.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

Then replace the code

<script type="text/javascript">
 $(document).ready(function(){
Dimensions = "sector_type";
Measures = "";
tblname = "sample";
data = $(this).serialize();

$.ajax({
method: "GET",
url: "http://localhost:5000/api/values/5",
headers: {
'Content-Type': 'application/json'
},
traditional: true,
async: false,
success:function(data){
chartdata = data.data;
alert("SUCCESS");
} 
});
});

</script>
Senthil
  • 687
  • 1
  • 7
  • 23
  • Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/...am i missing something ?? – Venky Dec 15 '16 at 06:02
  • Hi @venky async: true, – Senthil Dec 15 '16 at 06:10
  • thanks a lot,it seems to be working,but for some reason it says connection refused,im running this on linux – Venky Dec 15 '16 at 09:43
0

Try my code.. I think it will work fine

(function () {
    Dimensions = "sector_type";
    Measures = "";
    tblname = "sample";
    $.ajax({
        method: "GET",
        url: "http://localhost:5000/api/values/5",
        headers: {
            'Content-Type': 'application/json'
        },
        traditional: true,
        async: false
    }).success(function results(data) {
        chartdata = data.data;
        alert("SUCCESS");
    });
})();
  • if the JQuery library is not referenced in the page, the error will be the same with this piece of code :/ - moving pieces around does not seem to be the issue here – blurfus Dec 15 '16 at 05:31
  • He is not mentioned which file he inserted or not and you are right dude!! J query file must be added – Peeyush Kumar Dec 15 '16 at 05:38
0

Use $(document).ready(function(){});

<script type="text/javascript">

// This example loads the "Canadian Parliament 2012"
// dataset from a CSV instead of from JSON.        // System.import('app').catch(function (err) { console.error(err); });
$(document).ready(function()  { 
    Dimensions = "sector_type";
    Measures = "";
    tblname = "sample";
    $.ajax({
        method: "GET",
        url: "http://localhost:5000/api/values/5"
        headers: {
            'Content-Type': 'application/json'
        },
        traditional: true,
        async: false,

    }).success(function results(data) {
        chartdata = data.data;
        alert("SUCCESS");
    });
});

</script>
Pang
  • 8,605
  • 144
  • 77
  • 113
  • if the JQuery library is not referenced in the page, the error will be the same with this piece of code :/ – blurfus Dec 15 '16 at 05:32
0

check whether you've included the jquery library in your html code.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
kumaranc
  • 95
  • 1
  • 10
0

$ is not defined means you have not added jQuery plugin in your page.

Download jQuery plugin from jquery.com and add in your project. Reference that file in your page header.

For displaying data, it depend on what data you are retrieving. According to return data add control and bind.

$(function()  {

    Dimensions = "sector_type";
    Measures = "";
    tblname = "sample";

    $.ajax({
        method: "GET",
        url: "http://localhost:5000/api/values/5"
        headers: {
            'Content-Type': 'application/json'
        },
        traditional: true,
        async: false,
        success:function (data) {
        chartdata = data.data;
        alert("SUCCESS");
         }
    });
});
Pang
  • 8,605
  • 144
  • 77
  • 113