我使用express,node.js和mysql.Inside Ajax代码,成功无效.Below是ajax代码。
function GetData_1(){
var state = $("#dpState_1").val();
console.log(state);
$.ajax({
url:"http://localhost:5000/postDistrict",
type: "post",
data: {objectData :state},
dataType: "json",
crossDomain: "true",
success: function (result) {
console.log('Im reaching at the postDistrict');
$.ajax({
url:"http://localhost:5000/getDistrict",
type: "get",
dataType: "json",
crossDomain: "true",
success: function (result) {
console.log("Inside Success")
$.each(result,function(index,obj){
$("#dpDistrict_1").append("<option value = " + obj.dist + ">" + obj.dist+
"</option>"); });
},
error: function (obj, txtStatus, error) {
alert("There was some error,could not fetch data.....");
}
});
},
error: function (obj, txtStatus, error) {
alert("There was some error,could not fetch data... :((");
}
});
}
但是数据(即状态)会在所需的URL上发布。并且在node.js的帮助下,我使用POST获取帖子内容,然后使用GET将数据放入HTML中,代码是下面:
var application_root = __dirname,
express = require("express"),
mysql = require('mysql2');
path = require("path");
var app = express();
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123',
database: "info"
});
app.use(express.bodyParser());
app.use(express.cookieParser('shhhh, very secret'));
app.use(express.session());
// Config
app.configure(function () {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(application_root, "public")));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.post('/postDistrict', function(req, res) {
console.log(req.body.objectData);
var state = req.body.objectData;
app.get('/getDistrict', function(request, response){
res.setHeader('Content-Type', 'application/json');
console.log('select dist from information where state =' +"'"+ state + "'");
connection.query('select dist from information where state =' +"'"+ state +
"'", function(err, rows) {
response.json(rows);
});
});
});
// Launch server
app.listen(5000, function(){
console.log('Server running...');
});
答案 0 :(得分:0)
尝试在你的ajax中使用async:false
$.ajax({
type:'POST',
url:"ajax/cart.php",
async:false,
data:"prod_id="+$('#prodh_id').val()+"&qty="+$('#qty').val()+"",
success: function(result)