Trying to do my first nodejs project.
I have build a web-based sever using Nodejs, which runs at localhost:3000/home.
Then I use cordova built an android app and run on a emulator. I try to sent the query to the sever and get the search results back to show on the app. However, I get problems in using ajax and I have tried many expressions of the url, it all came out: file not found.
post function in homecontroller.js(using express and ejs)
app.post('/home', urlencodedParser, function(req, res) {
var rsults = [];
var pname = req.body.player_name;
var tname = req.body.team_name;
results[] = searchdatabase(pname,tname);
res.render('home', {results: results});
in the app: index.html
<form>
<input type="text" class="form-control" id="player_name"name="player_name">
<input type="text" class="form-control" id="team_name" name="team_name">
<button type="submit" class="btn btn-info" onclick="search">Search</button>
<script>
function search(){
var pname = $("[name='player_name']").val();
var tname = $("[name='team_name']").val();
jQuery.ajax({
type: "POST",
url: "http://localhost:3000/home",
data: '{"pname": "pname", "tname": "tname"}',
dataType: "json",
cache: false,
sucess: function(response)
{alert("sent");}
})
}
</script>
</form>
This code is poor. Maybe what I wrote in sever cannot corresponding to the ajax requst from the app or I am doing wrong with the ajax function. Please suggest me a right presentation to achieve the purpose.
ps. the web sever runs well.
via runchu
No comments:
Post a Comment