Render a view while Ajax expects a response - ajax

I am building an express project, using ejs as a view engine, and AJAX for front-end http calls.
When I post a request such this:
$.ajax({
type: 'POST',
data: {'nickname' : $('#nickname').val()},
contentType: 'application/x-www-form-urlencoded',
url: 'http://localhost:5000/create',
success: function(data) {
$('#message').text("Unkwon error");
},
error: function(data){
$('#message').text('Something went wrong, check connection!');
}
The Ajax keeps waiting for a response, which I am not willing to give, as I just want to render a view as follows :
app.post('/create', urlencodedParser, (req, res)=>{
let code = unique.generate(rooms);
res.render('chat', {'nickname' : req.body.nickname, 'code' : code}
Any ideas how can I work around this?

After some research, I found a way to do it.
Basically, I can just tell the Ajax to attach the document sent from rendering to the html body
$('body').html(data);
Surprisingly, this works event with Ejs dynamic tags.
I know it's not the best way to do it, but it's the only one I found till now.
EDIT 1:
After few months, I realized the solution is simple as just changing the Location using javascript
window.location.replace(`http://localhost:5000/newRequest`);
Now, I could handle this new request separately on the server.
EDIT 2:
After couple years now, I realized a GET request might have solved the problem in a single round trip.

Related

ckeditor insert or update doesnt work in first try

I'm having some trouble in using CKEditor and send it's information to database.
It works really good, I have all functionalities but when I sent it over ajax request to PHP, it doesn't assume the text into CKEditor first time. It only works after I make the ajax request the second time. It seems that CKEditor is not realtime and only after X time it saves the information that I typed, but I can't find anything related to it.
My init:
if (jQuery('#js-ckeditor:not(.js-ckeditor-enabled)').length) {
CKEDITOR.replace('js-ckeditor');
// Add .js-ckeditor-enabled class to tag it as activated
jQuery('#js-ckeditor').addClass('js-ckeditor-enabled');
}
My Function to make the request:
$.ajax({ url: '../inc/call.php',
enctype: 'multipart/form-data',
processData: false,
contentType: false,
cache: false,
data: formData,
type: 'POST',
beforeSend: function(){
$("#article_btn").html('<i class="fa fa-cog fa-spin"></i>');
$("#article_btn").attr("disabled", true);
},
success: function(output) {...}});
One more question, what's the best way to save this information to database?
Currently I'm using htmlspecialchars and htmlspecialchars_decode, but when using some functions (tables, colors), it really doesnt work well and don't save everything the correct way in database.
I will answer your first question:
If you are using AJAX please update textarea manually before sending data to server by calling https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-updateElement. This topic is actually known. Please see e.g. https://dev.ckeditor.com/ticket/9913#comment:2.

Speed up Sinatra page load with Ajax?

I am using the Sinatra to develop a web application but I am running into long page load time issue.
The application goes through and display a lot of data so I am sure this is the cause but I was wondering if there was a way to have the main erb layout of the page load first and then after that load, the data loads on top of it.
My first thought was AJAX but I am not sure how I would implement this.
Thanks in advance.
You are correct, it may help. The old mantra of "separate that which changes from that which does not" is applicable in any part of programming.
Here's route that returns some data:
require 'json'
get "/time-now/?" do
content_type :json
{time: Time.now.to_s}.to_json
end
and here's some javascript (jQuery ajax) to call it:
$.ajax({
accepts: "application/json",
type: "GET",
url: "/time-now",
dataType: "json",
success: function(res, status, xhr) {
return [$("#time").html(res)];
},
error: function(res, status, xhr) {
return [$("#time").addClass("error")];
},
complete: function(res, status, xhr) {
return [$("#button1").show()];
}
});
(that bit of jQuery may be a little bit wrong, just warning you, it's a copy 'n paste job hacked to fit this example)
Perhaps you could stop serving user pages from Sinatra routes and use static (i.e. prebuilt) pages with javascript in them that make AJAX calls to Sinatra routes. Or a mixture of the two types. Then you've got the start of a web service data API. There are lots of javascript frameworks to help with this, (e.g. Ember, Backbone, Angular and many more) and plenty of API builders alongside Sinatra, like Grape and Weasel Diesel.

ASP.Net MVC 3 VB.Net Ajax request gives me a 403 forbidden error

I've been trying to make an Ajax call from my vbhtml page and I can't seem to get it working. I've been researching for a while now my problem but I can't seem to find an answer, maybe it is because I don't actually know exactly what to ask.
In my code i am trying to send the value that was selected in a DataGrid from DataTables.net so that i can retrieve the information related to the selection and put it in some textboxes.
Anyways, I've been getting a 403 frobidden error when doing the ajax
Here's my View code
function ShowInfos(selected) {
$.ajax({
type: "POST",
url: "/Controllers/TelephonieController.vb/Show",
data: '{nomEcran: "' + selected + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
Here's my Controller code
<System.Web.Services.WebMethod()> _
Public Shared Function Show(nomEcran As String) As String
Return "allo"
End Function
(Sorry it's a bit in french)
This is the error it gives me
POST http://localhost:4390/Controllers/TelephonieController.vb/Show 403 (Forbidden)
I've only just started with Web so I might be a total newbie with this, but i have checked on the Web and people have been saying to take out ContenType or DataType and I've done both, I even tried sending and empty String with the Data but I can't seem to get it to work.
A bit off topic sort of, I tried an other way of doign ajax which is exactly this : http://msdn.microsoft.com/en-us/library/dd381533(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
the problem is I can't seem to understand how I should send or could send a javascript variable to the controller.
If someone has a better way of doing things than what I am doing right now feel free to comment so I can learn.
As you are using asp.net mvc3, you can try #Url.Action(...
url: "#Url.Action('Telephony','Show')",
You just need to provide controller and action name. Dont know why are you using TelephonieController.vb in url this isnt web form.
Though i dont have experience in vb, but still feels that vb would work like c#

Sensa Touch 1.1 with JSONP Object calls only the first time

I am trying to get an answer from a server through an Ext.util.JSONP.request. The code works fine and calls the callback function, but only for the first time. I have searched a lot about it, but the solutions I saw do not provide a solution for me. My Code is:
Ext.util.JSONP.request({
method: 'GET',
callbackKey: 'callback',
callback: function(data){
alert('data');
},
url: myUrl,
});
I tried adding changing params for every call in callbackparams as well as at the end of the url with ?param=myParam, being myParam different in every call.
I tried adding disableCaching: true
I tried to add to the response:
response.setHeader("Cache-Control", "no-store");
I also tried calling the url directly from the browser and this works fine, every time I call I receive a new answer, thus it is not a problem from the server.
I do not know what else could I test. Could you please help me?
Thanks a lot in advance.

Ajax result is getting encoded

I have a page where onload I issue several POST requests simultaneously in order to load widgets on the page. The request looks something like this:
$.ajax({
type: 'POST',
contentType: 'application/json',
dataType: 'html',
data: JSON.stringify({}),
success: function(htmlResult) {
$div.html(htmlResult);
}
})
Most of the time everything loads fine. However, some of the time one of the widgets will get loaded with a replacement character. It's not one specific widget that this happens to, but it could be any one of them at random.
When I inspect the bad HTTP response, I notice that the Vary header has a value of Accept-Encoding and the body is encoded. All the other responses have a value of * for that header, and the body is text/html. I'm not explicitly setting the Vary header value anywhere.
I can't figure out what's causing this random behavior. Any ideas?
FYI, I'm posting to an ASP.NET MVC action, and returning a partial view.
have you tried dataType:'json' instead of 'html'?

Resources