Browser-sync: Ajax post request doesn't work

Created on 17 Nov 2017  路  3Comments  路  Source: BrowserSync/browser-sync

Issue details

Hi guys!
I'm trying to get the .html file in my project folder and append it to another html page via ajax. But when i try to use jquery $.(post) method it says that the file is not found. But everything works great with the get method. So this code doesn't work:

$('[data-ajax1]').click(function (e) {
        e.preventDefault();
        $.post('ajax/popup-review.html', function (response) {
            $('body').append(response);
        });
    });

but this does:

$('[data-ajax1]').click(function (e) {
        e.preventDefault();
        $.get('ajax/popup-review.html', function (response) {
            $('body').append(response);
        });
    });

Here is the error i get:
POST http://localhost:3000/ajax/popup-review.html 404 (Not Found)

Thank you for responding!

Please specify which version of Browsersync, node and npm you're running

  • Browsersync [ 2.2.3 ]
  • Node [ 6.11.5 ]
  • Npm [ 5.5.1 ]

Affected platforms

  • [ ] linux
  • [x] windows
  • [ ] OS X
  • [ ] freebsd
  • [ ] solaris
  • [ ] other _(please specify which)_

Browsersync use-case

  • [ ] API
  • [x] Gulp
  • [ ] Grunt
  • [ ] CLI

If CLI, please paste the entire command below

{cli command here}

for all other use-cases, (gulp, grunt etc), please show us exactly how you're using Browsersync

var serve = {
    server: {
        baseDir: '.tmp',
        routes: {
          '/bower_components': 'bower_components'
        }
  },
  host: 'localhost',
  port: 3000,
  open: false,
    // tunnel: "geminis",
    logPrefix: "Geminis"
};
gulp.task('default', ['light'], function() {
    browserSync.init(serve);

    gulp.watch(path.src.styles + '*.scss', ['styles']);
    gulp.watch('src/ajax/*.html', ['ajax']);
    gulp.watch(path.src.html + '**/*.html', ['njk-watch']);
    gulp.watch(path.src.js + '*.js', ['scripts']);
    gulp.watch(path.src.img + '**/*.*', ['image:copy']);
    gulp.watch(path.src.img + 'icons/*.svg', ['svgsprites']);
});

Most helpful comment

This problem mybe most of HTTP server doesn't support post request for static resources.

But however, sometimes wo need do that, I use those codes to avoid this problem:

{
    'server': {
        baseDir: './',
        routes: {},
        middleware: function (req, res, next) {
            if (/\.json|\.txt|\.html/.test(req.url) && req.method.toUpperCase() == 'POST') {
                console.log('[POST => GET] : ' + req.url);
                req.method = 'GET';
            }
            next();
        }
    }
}

Maybe that is useful to you.

All 3 comments

I just use browser-sync in CLI, It has the some problem.

// it whorks well
$.ajax({
    url: './test/install.json',
    type: 'GET'
});


// it get 404
$.ajax({
    url: './test/install.json',
    type: 'POST'
});

Confirm with version 2.24.4.
AJAX POST requests return 404 for existing pages, tested with .html and .txt files.

This problem mybe most of HTTP server doesn't support post request for static resources.

But however, sometimes wo need do that, I use those codes to avoid this problem:

{
    'server': {
        baseDir: './',
        routes: {},
        middleware: function (req, res, next) {
            if (/\.json|\.txt|\.html/.test(req.url) && req.method.toUpperCase() == 'POST') {
                console.log('[POST => GET] : ' + req.url);
                req.method = 'GET';
            }
            next();
        }
    }
}

Maybe that is useful to you.

Was this page helpful?
0 / 5 - 0 ratings