0

Brand new to using JSon/ajax. Trying to replicate this jQuery UI Autocomplete using a static json file as source just as an example. I'm not positive I'm referencing this correctly if someone could let know whats wrong. Getting a (Uncaught ReferenceError: request is not defined)

<form id="searchform" method="get" role="search">
                <input id="searchfield" />
                <input type="submit" name="go" value="go!" />
            </form>


<script src='js/jquery-1.11.0.min.js'></script>
<script src="js/autocomplete/jquery-ui-1.10.3.custom.js" type="text/javascript" charset="utf-8"></script>

<script>
    $(function() {
    $.ajax({
        url: "json/Providers.json",
        dataType: "json",
        data: {term: request.term},
        success: function(data) {
            var cat_data = $.map(data, function(item) {
                return {
                    ProviderID: item.ProviderID,
                    Name: item.Name,                  
                };
            });
            $("#searchfield").catcomplete({
                delay: 0,
                source: cat_data,
                minlength:0
            });
        }
    });
});
</script>

json format

   {"Providers":[{"ProviderID":"3","NAME":"name1"}, 
    {"ProviderID":"4","NAME":"name2"},  
    {"ProviderID":"5","NAME":"name3"}]} 
Community
  • 1
  • 1
user1
  • 347
  • 2
  • 12
  • 22
  • 1
    request is not defined. So what are you expecting request to be?! From where you copied this code you should be able to find where `request` is defined... – A. Wolff Jan 13 '14 at 15:08
  • 2
    The message is pretty clear: `request.term` (request is not defined) – Merlin Jan 13 '14 at 15:08
  • is 'request' a function in the jquery file? What do I need to replace it with? – user1 Jan 13 '14 at 15:09
  • @user1 Request is an object, test by replacing it by a string firstly: `{term: "anything i want to search for"}` But if you have no idea what you are looking for, please start by reading some basic tutos – A. Wolff Jan 13 '14 at 15:13

2 Answers2

0

This particular data: {term: request.term}, is the problem

You need to define any javascript variable before accessing them.

 var request;

In your case data is not required. Just remove data: {term: request.term} since you are loading the static .json file

Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
0

Since you're a reading json file, it is clear there is no need to pass any parameter. So remove data: {term: request.term}

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • get more errors removing it: OPTIONS file:///C:/Users/jswanson/Desktop/Survey/json/Providers.json No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. – user1 Jan 13 '14 at 15:19
  • also this one: XMLHttpRequest cannot load file:///C:/Users/jswanson/Desktop/Survey/json/Providers.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. – user1 Jan 13 '14 at 15:21
  • 1
    Ajax is the act of making HTTP requests from JavaScript. Stop trying to do it without HTTP. Run a web server. – Quentin Jan 13 '14 at 15:21
  • @user1 this is called CORS.. you can overcome by changing dataType from json to` jsonp` or nove on to web servers. – Praveen Jan 13 '14 at 15:23
  • only change the dataType and nothing else? still getting similar errors. – user1 Jan 13 '14 at 15:27
  • @user1 you're trying to call local file via your js file. Hence you're suffering from CORS. If you're already hosting in web server put this json file there. – Praveen Jan 13 '14 at 15:31
  • I understand however I'm not hosting a web server. This is being done locally. Changing to jsonp still gives me those errors if everything thing else is correct i assume. – user1 Jan 13 '14 at 15:32
  • @user1 remaining code is right.. also here ia question which is closely related http://stackoverflow.com/questions/19865938/unable-to-load-xml-from-external-file-using-jquery – Praveen Jan 13 '14 at 15:40
  • I think the problem was chrome I do not see the same warning in explorer: http://stackoverflow.com/questions/5396527/error-origin-null-is-not-allowed-by-access-control-allow-origin-when-loading – user1 Jan 13 '14 at 15:41