繁体   English   中英

在Ajax代码内部,成功不起作用

[英]Inside Ajax code ,success is not working

我正在使用express,node.js和mysql.Ajax代码内部,成功不起作用。下面是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...');
  });

尝试在您的ajax异步中使用:false

$.ajax({
        type:'POST',
        url:"ajax/cart.php",
        async:false,
        data:"prod_id="+$('#prodh_id').val()+"&qty="+$('#qty').val()+"",
        success: function(result)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM