简体   繁体   中英

Inside Ajax code ,success is not working

I m using express,node.js and mysql.Inside Ajax code ,success is not working.Below is the ajax code.

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... :((");
          }
        });
       }

But data(ie state) is getting posted at the required URL.And further with the help of the node.js, im fetching the post content using POST,then using GET to place the data in HTML and the code of is below:

 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...');
  });

try to use in your ajax async: false

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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