我正在使用NodeJS对MongoDB执行som查询。
时我已经设置了路由 http://127.0.0.1:8888/GetAllCities
调用,函数名称GetAllCities将在服务器上运行并执行MongoDB查询并返回结果。 当我尝试直接在浏览器中访问该页面时,这很好用。 但是当我尝试发出CURL请求时,响应为空。
这是我的jquery Ajaxcall
function makeCall(urlToCall, method)
{
var params = "url=" + urlToCall + method;
if(app === "PHP")
{
params += "&PHP=PHP";
}
$.ajax({
type: "POST",
url: "phpProxy.php",
data: params,
beforeSend: function()
{
document.write(app + ' -- Starting round ' + (i+1) + "<br />");
document.write('URL: ' + params + "<br />");
startTime = new Date().getTime();
},
success: function(data) {
endTime = new Date().getTime();
totalTime = endTime - startTime;
if(totalTime > highest)
{
highest = totalTime;
}
if(totalTime < lowest)
{
lowest = totalTime;
}
var row = {};
row.app = app;
row.operation = method;
row.totalTime = totalTime;
row.run = i+1;
row.date = new Date();
document.write('\tTotal time: <strong>' + totalTime + 'ms</strong>' + "<br />");
times.push(row);
document.write(data + "<br />");
document.write('Round done\n' + "<br />" + "<br />");
},
error: function(err) {
console.log('Error');
console.log(err);
},
complete: function()
{
i++;
if(i < 50)
{
makeCall(urlToCall, method);
}
else
{
var sum = 0;
for(var j = 0; j < times.length; j++)
{
sum += parseInt(times[j].totalTime);
}
var avg = sum/times.length;
$('body').prepend('<h3>Slowest: ' + highest + 'ms</h3>');
$('body').prepend('<h3>Fastest: ' + lowest + 'ms</h3>');
$('body').prepend('<h3>Average: ' + avg + 'ms</h3>');
myApp.saveToDb(times);
}
}
});
}
这是我的PHPPROXY文件:
$method = $_SERVER['REQUEST_METHOD'];
$url = $_POST['url'];
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_POST, TRUE);
if(isset($_POST['PHP']))
{
$params = array('a' => 'php');
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
这是我的NODEJS requesthandlerfile。
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var querystring = require("querystring");
function GetAllCities(response)
{
console.log('Request recieved');
var databaseUrl = "exjobb";
var collections = ["cities"]
var db = require("mongojs").connect(databaseUrl, collections);
db.cities.find(function(err, cities) {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(cities.length + " rows selected from the Database");
response.end();
db.close();
});
}
function GetAllCitiesWhere(response)
{
var databaseUrl = "exjobb";
var collections = ["cities"]
var db = require("mongojs").connect(databaseUrl, collections);
db.cities.find({state: "AL"}, function(err, cities) {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(cities.length + " rows selected from the Database");
response.end();
});
}
exports.start = start;
exports.GetAllCities = GetAllCities;
exports.GetAllCitiesWhere = GetAllCitiesWhere;
TLDR;我的代码在浏览器中访问页面时可以直接使用GET,但是在发出ajax请求时却没有,为什么? 您可以找到所有代码here
答案 0 :(得分:0)
如评论中所述,这已得到解决。
问题出在我的卷曲请求中。
curl_setopt($curl,CURLOPT_POST, TRUE);
我将PHP代理更改为以下设置:
$method = $_SERVER['REQUEST_METHOD'];
$url = $_POST['url'];
$curl=curl_init();
curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,CURLOPT_USERAGENT => 'Josefs PHPPROXY'));
$result = curl_exec($curl);
curl_close($curl);
echo $result;