0

I've searched around the web and such but so far unsure of how to fix my problem as they seem to delve into other areas and not focused on mine.

So, I'd like to know the best way of retrieving a record or records from a database using jQuery and Ajax, and without using PHP.

I am able to retrieve the whole database with this code

function getRecords(){
var url = 'http://myurl/reports?callback=?';
  $.getJSON(url, null, function(data){
   $.each(data, function(key, value){
    value.date;
    value.area;
    value.details;
    value.number;
    })
  });
};

(That code is not the full code, just an example of it). And I can access the required data using the above value.var.

I can also send/update/delete records to the database, here's an example of my deletion code

function DeleteRecord(){
  $.ajax({
    crossDomain: true,
    method: 'POST',
    url: 'http://url/delete',
    data: {"ID": newID},
    success: function(cb){
      console.log("ID to be deleted is : " + newID);
   },
    error: function(){
      alert('error');
    }
  });
}

API for the deletion:

restapi.post('/delete', function(req, res){
  var delID = req.body.ID;
  db.run("DELETE FROM database WHERE Id='"+delID+"'");
});

My problem occurs when I need to send a request which I assume would be a .get request to the server which would pass across the search variable, and then retrieve the records from the database based on the variable

so the sql statement would be something like

("SELECT * FROM database WHERE number='"+variable+"'", function(err, data)

I just can't seem to grasp whether this should be a get request, a post request, a post request with an inserted get request.

I am using sqlite for the database, javascript, jQuery and Ajax.

Any help would be appreciated on where to start with this, as examples I have seen implement PHP and that's not what I am using.

3
  • You can use either GET or POST, it's entirely your decision. Commented May 2, 2016 at 23:56
  • It seems to me that when I try and code it, that I cannot pass in a variable when using GET, and when using POST I'm not retrieving anything Commented May 3, 2016 at 0:22
  • You said you are not retrieving anything when using POST, so what did the error response return? Your ajax request might be expecting some other datatype that the server does not coincide so you might consider checking on it.. Commented May 3, 2016 at 0:47

1 Answer 1

1

Due to not thinking what the response in the request I solved it by doing the following:

function getRecords(){
  uNumber = $("#userNumber").val();
  $.ajax({
    crossDomain: true,
    method: 'POST',
    url: 'http://url/userReports',
    data: {"Number": uNumber},
    success: function(data){ //It was here I wasn't thinking about correctly
      console.log(data);
      $.each(data, function(key, value){
        console.log(value.area);
        area = value.area;
      })
      //I then displayed the returned data
      document.getElementById("Table").innerHTML = area;
    },
    error: function(){
      alert('error');
    }
  });
};

This worked for me, it's probably not the best way of doing it, but it works

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

2 Comments

And please do highlight the area where you have connected to a mysql server in this code and where you are executing the query.
For what I have I guess that would be in the api and that code is restapi.post('/userReports/:number?', function(req, res){ var num = req.body.Number; db.all("SELECT * FROM database WHERE number='"+num+"'", function(err, data){ res.jsonp(data); }); });

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.