API Expose (NodeJS/ExpressJS) - javascript

I'm writing a website on Express. Handling routes using express router. I have some routes: registration, login, upload, and so on. As upload route expects files, in front-end I use ajax to upload files to upload route. The question is: am I allowed to reveal route names in front end. As in front end:$.post('upload')...

Yes, you can call routes with ajax, somelike this...
$.ajax({
type: 'POST',
data: JSON.stringify(data),
cache: false,
contentType: 'application/json',
url: '/route',
success: function(ret) {
// here its possible to get the return of res.send() from server
}
});
But, one tip for to use upload with express: search for formidable and formidable-upload for facility your work, somelike this =]

Related

why 404 and 405 shows on my live laravel project when I use ajax post request

I have developed simple cart system in laravel for my local client, it has no any issue on my local server but when I publish it on my live server which is infinity free hosting it has multiple issues. When I try to add to cart the product on home page it is showing 404 error, I am using ajax request.
JS CODE:
jQuery.ajax({
url: "/add_to_cart",
method: "post",
data: jQuery('#formAddToCart').serialize(),
success: function(result){}
my home page url is "new_shop/" and when I remove the "/" from my js code url it is working perfectly but then it shows error on my product detail page where I am using same function and the url of product detail page is "product-name" like this without "/"; product detail page add to cart is also not working with "url: "/add_to_cart"," this url. I don't know what the confusion is as it is working perfectly on my local server. I think it may be issue of free hosting. Any assistance will be appreciated.
ROUTE WEB.PHP CODE:
Route::post('add_to_cart',[FrontController::class,'add_to_cart']);
You are doing it in a wrong way from js code.
You should use name for your routes like this
Route::post('/add_to_cart',[FrontController::class,'add_to_cart'])->name('cart.add');
Now call it from the js code like this
jQuery.ajax({
url: "{{route('cart.add')}}",
method: "post",
data: jQuery('#formAddToCart').serialize(),
success: function(result){}
})
As I am not sure you have group for your route or not
You can try this also
jQuery.ajax({
url: "{{URL::to('/add_to_cart')}}",
method: "post",
data: jQuery('#formAddToCart').serialize(),
success: function(result){}
})
And make sure that you are passing csrf token in your form data.
This way you don't need to worry about your methodName as well as your url. You just need to provide a name to your routes

How to pass variables in a get request without showing it in the url - nodejs

I want to make a get request to render a page but I need to pass 2 variables to that page. How can I create a ajax request to do this without having the variables show up in my url such as var1=?var2=?.
$.ajax({
async: true,
type: 'POST',
url: '/profile',
data: {
'UID': currentUid,
'Name': user_data.displayName
},
success: function (res) {}
})
I thought of creating a post request and passing the variables that way but then how do I render the page that I want to because my Post request doesn't fetch the page
In express you should get the page using
app.post
instead of
app.get
If you do for example:
var express=require('express')
var app=express()
app.get('/profile')
Your app will not fetch the page using POST method. Only GET method.
This might be your issue.
More info:
App req.METHOD()

AJAX request fails .zip file OnceBuilder CMS

I'm building another full stack library but i'm so strugling on one AJAX request, which is so ridiculous. I have just found that, this function is strugling to get file archive but it can gets easly another files ( that can be solution but... i need zip file too) plugin 19 is static because it's just testing and only this file exists
#file_get_contents('https://oncebuilder.com/once/plugins/19/plugin.zip');
So basicly both AJAX function are the same
If i lunch the same function from preview mode file_get_contents gets crazy and return error, but if i lunch it from the listing or via browser its ok
AJAX function are both the same
$.ajax({
type: 'POST',
url: once.path+"/ajax.php?c=plugins&o=item_download&id=19",
contentType: "application/json",
dataType: 'json'
})
.error(function() { console.log("Request Error: item_download"); });

jquery $.ajax gives 404 not found. Im using node.js with express

This jquery:
$.ajax({
method: "post",
data: JSON.stringify(data),
contentType: "application/json",
url: "http://localhost:3000/ajax"
});
is giving error 404 not found. Here is my server side:
router.get('/ajax', function(req, res ,ext){
var strings = ["rad", "bla", "ska"]
console.log('body: ' + JSON.stringify(req.body));
console.log("AJAX RECEIVED");
res.send(strings);
});
so i do have the /ajax route. When i go to http://localhost:3000/ajax im able to acccess the site . However, when I try to access it with the ajax method I get the 404 error. So im wondering if the error could be with my code, or if it could be the firewall on my computer. Im actually using a company computer that has a firewall that blocks certain sites and I cannot disable it.
If you want to do a HTTP POST, use router.post, not router.get.
your $.ajax call is using POST but your route in express.js is only listening for GET requests.
When you hit the URL through the browser it's doing a GET, so it worked.
You would need to set up a route for POST with router.post('/ajax', ...)

send and receive data using ajax express node

I am new to node Express. I am trying to upload a file using ajax.. but I don't know the best method to do so. I have seen many tutorials.
some are written post code to the app.js file and some in routes/index.js some say directly writing in the app.js file is not a good approach. I wasn't able to run it using routes kindly tell me which is the better approach to do so. and how to do it.
thanks
Ajax Call
var formData = new FormData($('#cover_file_form')[0]);
$.ajax({
// url: '/cropImg',
url: 'http://localhost:3000/cropImg',
data: formData,
//contentType: 'application/json',
type: 'POST',
success: function(data){
alert(data.file_name);
},
error:function(dat){
alert('error'+dat.file_name);
}
});
App.post()
app.post('/cropImg',function(req,res){
// get the temporary location of the file
var tmp_path = req.files['file_browse_cover'].path;
//i have skipped the extra code
es.send({file_name: req.files['file_browse_cover'].name});
}

Categories

Resources