How to implement ajax in node.js to achieve partial postback - node.js

I am using node.js (express framework and hbs engine) and mongodb to develop an application.
I want to implement an ajax call to to page for partial post back.

As mentioned by others in comments, the best approach for this is described in Ajax to refresh a partial view using express and JQuery?

>*pass ur URL,and parameter to this function:It is working fine....*
function ajaxRequest(url, succesCallBck, completeCallBck, errorCallBck)
{
$.ajax({
url: url,
type: "POST",
dataType: "json",
complete: completeCallBck,
success: succesCallBck,
error: errorCallBck
});
}

Related

call node.js file from ajax call in phonegap?

I want to call the node.js file through ajax call but unable to did, help me to understand which type of URL will be entered in ajax to call the node.js file.
If you are using Jquery on the client side
you can make the request this way :
$.ajax({
url: 'your-url',
success: function(data) {
//it works, do something with the data
},
error: function() {
//something went wrong, handle the error and display a message
}
});
You can read more details here PhoneGap and JSON API

Can i use AJAX request to update the whole page like reqular http requests?

If i can update the whole page with AJAX when and why should i use regular http methods?
Can AJAX requests replace normal http methods?
i am using nodeJS expressJS
Yes, you can update the whole page using AJAX. Send request from user end and get back an JSON array from server containing all the information you want.
If you want to load data from .txt file or .html, then
$.ajax({ url: 'http://website/some'
, type: 'GET'
, dataType: 'html'
})
.done(function(data) {
$('#container').html(data);
})
.fail(function() {
console.log("Something went wrong!");
});
Just wondering why you are looking at AJAX when you need a complete reload.
Yes, definitely you can. But You will not ask this question once you know the use of AJAX.
For example, you will never ever update the whole page if you are using AJAX. For example, at least Navigation will remain the same and the body part will change. This is the use of AJAX and many web applications load like this. They keep navigation the same and then update rest of the web page.
You will have this kind of application where you will update the body of the page. You can use REST API to fetch the data in the form of JSON and then load this data using styling and Javascript framework like Angular, React, Backbone, etc.

Getting cross-origin error

I am trying to make following ajax request:
$.ajax({
method: "GET",
url: http://google.com,
dataType: "json",
async: false,
cache: false,
success: function(data) {
alert("AJAX call successfully
completed");}});
But as expected, I am getting "Cross-Origin" error. Is there any way to bypass this security and make the ajax call without this error even if ajax call is being made from different domain.
Any response is appreciated.
You can use a browser plugin that will allow you to request any site with ajax from any source
Chrome example:
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

Constructing AJAX url values from a ServiceStack-backed SPA

I have an ASP.NET MVC 4 application, that serves as the host for my ServiceStack AppHost. Everything is working fine in development, but in my first test deployment, I realized I neglected to handle differences in my AJAX url values between my environments (development/test/staging/deployment). My app is really just a Knockout-based SPA that makes calls to ServiceStack, so MVC merely provides the hosting.
So my javascript has this:
self.DoSomething = function (event) {
// here is my problem. the service url is
// specific to the current environment
var service_url = '/api/some_method';
$.ajax({
url: service_url,
dataType: 'json',
type: 'POST',
success: function (data) {
// do something
}
})
};
In a 'normal' ASP.NET MVC app, where I'm calling controller actions, I'd do this:
var service_url = '#Url.Action("MyControllerAction", "MyController")'
The best solution I have so far is this:
var service_url = '#Url.Content("~")api/some_method';
It works fine, but it looks uglier than I would have thought possible for a single line of code. It also looks like the sort of thing that could get messed up without noticing until the AJAX calls start failing...
My inclination would be to wrap this up into a js helper file, which could be referenced from any similar page, but the js helper wouldn't get resolved by the razor engine.
Does anyone know if there is a 'best practice' way to do this?

How to dynamically render/load pages in express?

I need to dynamically load/render part of a page in nodejs (v1.8.15) with express (>3.0) framework. Generally, I want to create a single-page app.
I have a menu at the top of the page with links. Clicking on the links will change the content below, as in AJAX page loading.
For example:
>home|login|signup|chat
..content for home..
If I press the 'signup' link:
home|login|>signup|chat
..content for signup..
In express I have routes on the server:
var express = require('express');
var app = express();
app.get('/signup', function(req, res) {
// render signup.jade
res.render('signup');
}
app.post('/signup', function(req, res) {
// .. work with information
if (ok) res.send('ok', 200); else res.send(error, 200);
}
After reading this, I figured out that I should use socket.io. I know sockets well, so it will be easy to send data about 'clicking on link' from the client to the server.
Q1: How do I render/load pages dynamically like I wrote in express?
Yes, I could use AJAX for page loading, but will it work for .post methods in express?
How should I organize my thoughts to create such a site?
By the way, I've read about Derby and SocketStream, but I didn't understand.
Q2: Can I use Derby or SocketStream in my aims (site functions: login, signup, chat)? How?
If SocketStream is what I need, that would be very bad, because Heroku doesn't work with it.
Q1) This is in fact very simple, no need for Socket.io, Derby or whatever. You can call any expess route with any method through ajax, using jQuery makes ajax very easy. In your example, let's suppose your container HTML file has a div with id 'container', which is where you want the ajax-loaded content to go:
$.ajax({ url: 'http://yoursite.com/signup'
, type: 'GET'
, dataType: 'html'
})
.done(function(data) {
$('#container').html(data);
})
.fail(function() {
console.log("Something went wrong!");
});
Express supports all HTTP verbs (GET, POST, PUT etc.). For loading pages dynamically, use GET, then when a user enters some login information you can POST it to an Express route that will tell you if it is valid or not, and you use jQuery to modify the DOM accordingly.
Q2) As said in Q1, no need to use Derby or SocketStream. Plain old jQuery + basic Express will get you where you want!

Resources