[英]Making ajax call to node server
I am new to NodeJS and trying to use npm spotcrime package to get crime data from user input location on click of a button through ajax call. 我是NodeJS的新手,并尝试使用npm spotcrime包通过ajax调用单击按钮时从用户输入位置获取犯罪数据。 Here is the usage of the package given on npm documentation page of package.
这是软件包的npm文档页面上给出的软件包的用法。
var spotcrime = require('spotcrime');
// somewhere near phoenix, az
var loc = {
lat: 33.39657,
lon: -112.03422
};
var radius = 0.01; // this is miles
spotcrime.getCrimes(loc, radius, function(err, crimes){
});
which works perfectly fine when run separately and returns correct crime json data. 当单独运行并返回正确的犯罪json数据时,它的工作原理非常好。 However I don't understand how to call this through ajax call on click of button.
但是我不明白如何通过单击按钮的ajax调用来调用此方法。 Any insights would be really helpful.
任何见解都将真正有帮助。 Thanks in advance :)
提前致谢 :)
UPDATE 更新
Here is my server code 这是我的服务器代码
var express = require('express');
var app = express();
var dataFile = require('./data/data.json');
var reload = require('reload');
var path = require('path');
app.set('port', process.env.PORT || 3000);
app.set('appData', dataFile);
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.static(path.join(__dirname, '/public')));
app.use(require('./routes/index'));
app.use(require('./routes/information'));
app.use(require('./routes/spotcrime'));
var server = app.listen(app.get('port'), function(){
console.log('Go to http://localhost:' + app.get('port')+ ' on your
browser');
});
reload(app);
And here is spotcrime.js file 这是spotcrime.js文件
var express = require('express');
var router = express.Router();
var spotcrime = require('spotcrime');
router.get('/spotcrime', function(request, response){
var loc = {
lat: 33.39657,
lon: -112.03422
};
var radius = 0.01; // this is miles
spotcrime.getCrimes(loc, radius, function(err, crimes){
response.send(crimes);
});
});
module.exports = router;
I am trying this from quite a while but nothing shows up. 我已经尝试了好一阵子,但没有任何反应。 Please suggest any way out
请提出任何建议
use this code to make ajax call 使用此代码进行ajax调用
var http = require('http');
var request = require('request');
request.post({
url: "xxxxxxxxxxxx",
method: "POST",
json: true, // <--Very important!!!
body: {}
},
function(error, response, body) {
if (!error) {
console.log(body.d);
} else {
console.log(error);
}
}
);
If I understood correctly you want to send an AJAX request from a browser(front-end) to your server. 如果我理解正确,则希望将AJAX请求从浏览器(前端)发送到服务器。 This can easily be done with jQuery AJAX see the official documentation page .
使用jQuery AJAX可以很容易地做到这一点,请参阅官方文档页面 。 Here is an example for you:
这是给你的一个例子:
<buttom class="send-request">Send AJAX request</button>
$(".send-request").click(function() {
$.ajax({
method: "GET", // since you are waiting for GET request on server
url: "/spotcrime", // you might have to provide full path to your endpoint starting with https://127.0.0.1/spotcrime
data: { Greetings: "Hello" } // data object can be deleted since you don't expect any parameters on server side
})
.done(function( msg ) {
alert( "Response data: " + msg );
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.