0

My goal for my code is to execute a json query request using a textfield to designate the location it is a quick search based text searching a specific website that is different from the location

eg: if i type in www.calsolum/abc i want it to search the website calsolum for 3 json files:

'a', 'ab', 'abc' the last one being the actual json file

my index file looks like this

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
</head>
    <script src="jquery.js"></script>
    <script src="script.js"></script>
    <input type="text" id="location" onkeyup="myFunction()">
    <body>
    </body>
</html>

the script.js looks like this

var myFunction = function(event) {

var payload = {location: $(this).val()};

var jqxhr = $.getJSON('/location', payload);

jqxhr.done(function(data) {
   console.log(data); 
});
};

// jQuery on DOM ready
$(function() {
  $("#location").keyup(myFunction);
});

So far it seems as though i get the right requests made but going from the errors the program is looking in the wrong location Any help would be greatly appreciated. If any clarification is required do not hesitate to ask.

EDIT2: so i've implemented the edits suggested and my new error when running it is

Uncaught TypeError: Cannot call method 'toLowerCase' of undefined

and

 GET http://mobile.sheridanc.on.ca/location?location=http%3A%2F%2Fwww.sightlineinnovation.com%2Fsyst35288%2FAPI%2Fv1.0%2Fexchanges%2FWSA 404 (Not Found) 
4
  • Well, you're declaring a variable x inside a function. It's a local variable, so it's not available outside the function. Commented Jul 31, 2013 at 2:04
  • You want to do a getJSON request for each keystroke ? Commented Jul 31, 2013 at 2:08
  • You should either define var x outside the function or return it. That's a local variable and it's scope is the function. So basically you're function storing some data in a variable which won't be accessible afterwards. Plus your html tags are messed up. You have script and input between head and body. Commented Jul 31, 2013 at 2:12
  • okay so with everyone's suggestions i moved the JSON requestion to inside the function but theres an Illegal invocation error and ive no idea what that means. and to clarify i'd like the request for each keystroke but i also want it to append the previous keystrokes eg: for if i type car i want it to make 3 requests: c, ca, car. Commented Jul 31, 2013 at 2:22

4 Answers 4

2

This should help you

var myFunction = function(event) {

    var payload = {query: $(this).val()};

    var jqxhr = $.getJSON('/path/to/location_query', payload);

    jqxhr.done(function(data) {
       console.log(data); 
    });
};

// jQuery on DOM ready
$(function() {
  $("#location").keyup(myFunction);
});

Each time a user types something in the <input>, this will make a getJSON request with the following payload {query: X} where X is the value of the input.

Sign up to request clarification or add additional context in comments.

5 Comments

Look like he want to do quick search based text rather than sent out which key end user pressed. I think his question is not quite clear.
ok i've tried the code exactly as youve written but i get 2 errors First is: Uncaught TypeError: Cannot call method 'toLowerCase' of undefined Second is: GET mobile.sheridanc.on.ca/… 404 (Not Found) my json file is stored on different website so i think that complicates things because it seems to be looking for the file on the local website
@Calsolum, if you wrote code exactly as I did, you wouldn't see a toLowerCase error; nowhere in my code does that function exists. Second, /location is just an example path; you have to use a request path that matches one your server is using to process your request and return JSON. I made an edit that should make this a little more clear.
@TelvinNguyen, yeah I had that as the second part of my answer. I updated it to make things a little more clear thoguh.
Thanks for your help, it was a mistake on my end as you pointed out and it now works as need it to
1

I would recommend binding the event via jQuery like so, so you have a standard way of getting the event data:

$("#location").on("keyup", myFunction);

Also, doing this, you must get rid of the onkeyup="myFunction()" on the input element.

Change your myFunction function to accept an argument:

function myFunction(event) { .... }

Then, you can check which key code is pressed by checking:

var keyCode = event.which; // example usage, returns a number indicating the keycode

Hopefully, this helps you get closer to what you need

For the JSON - you need to call $.getJSON inside the event handler, putting it out side of myFunction() will only run run once outside of the event. You may want it to run every time the event triggers.

Comments

1

From your example it looks like the variable 'x' is closed in the function 'myFunction', you might want to move the variable into the function body ie:

function myFunction(){
    var x=document.getElementById("location");

    //Moved getJSON inside 'myFunction' and added 'x' as second JSON param
    $.getJSON('location', x, function(data) {
        console.log(data);
    });
}

Comments

1
function myFunction(){
    $.getJSON('location', {mylocation: $('#location').val()}, function(data) {
        console.log(data);
    });
}

Each time user type one word, it makes a request such as

[your_server]/location?mylocation=[what user typed on location field]

And then on your server, just get "mylocation" as querystring.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.