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!
{cli command here}
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']);
});
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.
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:
Maybe that is useful to you.