I am working with codeigniter and jquery. I am using ajax to send some info to a codeigniter function to perform a db operation , in order to update the page.
$.ajax({
type: "POST",
url: BASE_URL+"Update/update",
data:{ i : searchIDs, m : message },
dataType: 'json',
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
},
complete: function() {
alert("REFRESHING..");
window.location.href = "pan_controller/reply";
}
});
After the operation is complete I want to reload the page. This works normally in locally on my WAMP. However when I deploy to a shared host I usually have to reload the page manually, (using the reload button in the browser -- screenshot above). I see no errors in firebug. I've tried a variety of fixes but have recently started to wonder if this is a caching issue on my shared hosting server. If so is there a better way to reload using jquery rather than just redirect so as to avoid a cache on the shared host?
Have you established that the complete function is running in those cases when the page doesn't reload? If so then I think you are right, it's a caching problem.
You can try adding a timestamp parameter to the end of the window.location.href value to make it unique each time and avoid any issues with caching, although a better approach would be to send the correct headers back with the response, so that the browser knows not to cache that page.
window.location.href = "pan_controller/reply?t=" + (new Date().getTime());
Related
We are a social start-up and are given a great opportunity by one of the biggest banks in our country. Basically they will feature our website in their Mobile App, and users can access our website via their app in an iFrame. We are currently working on integrating a SSO log in flow between their app and our website, so users are logged in immediately when they open our website in their app.
For this, they have created an API that we're able to use. The API requires 2 main things to get the flow working:
send a pageLoaded() event when the DOM is ready, when this event is sent, we get returned a token to fetch the user's personal information with a public event.
send a pageReady() event when the backend logic processing is done (eg account created and user logged in).
They will show a spinner in their app, until the pageReady event is being sent to them.
My website uses PHP and JS (jQuery) as the main technologies. I am having a number of issues on how to implement this correctly.
This is the code I am using as of now, it works in a certain way, but it's very troublesome for the reasons mentioned below the code snippet:
$(document).ready(function(){
var getUrl = window.location;
var baseUrl = getUrl.protocol + "//" + getUrl.host;
/*** Mobile App JS Code ***/
var receiver = new PostmessageExternalReceiver();
var sender = new PostmessageExternalSender();
receiver.addTokenEventListener(function (api, token, version){
if (api == 'init') {
$.ajax({
type: 'post',
cache: false,
url: baseUrl + '/login/abc/abcLogin',
data: {
'token': token
},
dataType: 'json',
success: function(response){
sender.pageReady();
window.location.replace(response.redirect_url);
}
});
}
});
sender.pageLoaded();
});
These are the problems that i'm not sure how to get around:
Since the sender.pageLoaded(); is in the jQuery document ready, and after performing the AJAX request, we are redirecting again to the same page (the homepage), because after the redirect, the user will be logged in and some extra blocks will show on the homepage. So if we are on the homepage again, the document ready will fire yet another sender.pageLoaded() event, and we are stuck in an infinite redirecting loop.
Right now I am including all 4 API javascript libraries provided by our third party, and my own javascript file including the AJAX request and the pageLoaded() and pageReady(). This is only applicable for users that come to our website via the 3rd party mobile app. For all other visitors in our website this is not applicable and requires extra resources to be loaded in when they're not used. (the ajax request will never be executed for them because a token will not be sent via the mobile app, but still we do not want to send a pageLoaded() for every visitor on our website, even if he is not from the mobile app.
This is a nice-to-have question: since we need to send the pageReady() after our PHP logic (via AJAX request) is done, we cannot do the redirect in PHP, so I return the redirect_url in my AJAX and then do the window.location.replace() method with the redirect URL. In the 3rd party app, this will cause the app to remove the spinner (on the pageReady() event) and show our website, but immediately afterwards we will redirect the user using the window.location.replace() , this causes a refresh of the page, and thus a small annoyance and not so smooth experience for the user. They expect after the spinner is gone that the website is immediately accessible. How would I go around this?
Thanks a lot in advance!
I was calling an ajax function on load page event. When I load the page, in the server log, There was not existing call log(I log entrance and out on all mvc server method). The request from javascript to server was taking some time, 2 to 3 minutes. The strange thing is when I test it local and test server, it works well. It occurs just when I deploy a project to the real server!
I found some posts about the method to do ajax, xmlhttp, $.ajax(). I already used both. But It still exists.
This is my javascript code.
$(document).ready(function () {
$.ajax({
type: "GET",
url: allservicesUrl,
async: true,
success: function(result, status, xhr){
var services = JSON.parse(xhr.responseText);
for (i in services) {
createServicecard(services[i]);
}
},
error: function(xhr, status, err) {
alert(xhr.responseText);
}
});
})
I wanna execute it immediately. How do I correct this problem?
Thanks for the advice.
I saw an answer and comments. Then, I saw the dev tool(network tab), finally, I found the problem.
Now I correct it, it works well. Thank you very much.
ps. Problem: connecting the internet in closed network.
Use the debugging/developer tools in your browser to trouble shoot this issue.
look in the console and see if you have any JS errors, then look in the network tab. Clear the entries and then reload the AJAX call.
you should be able to see if your script is slow to send the request to the server or if the server is slow to answer.
until you figure out if the bottleneck is in the script or on the server you can't fix it.
I want to time how long a visitor spends on each page of my website by downloading and analysing the "ex" log file created daily by Apache in the logfiles folder.
I can do this when they move from one page to another but if someone visits the homepage and then leaves I have no idea how long they spent on it.
Could I somehow use Javascript's OnBeforeUnload event to request a file from the server, which would then appear in the log?
I am not sure why do you want to request a file from the server. You could just request a blank page:
$(window).unload( function () {
$.ajax({
async: false,
type: 'GET',
url: '/url/to/blank/page'
});
}
I’m building a phonegap application using Jquery mobile, I use ajax calls for communicating with tomcat server. All my scripts are located within the application. My first ajax call works and my second ajax call does not work for Windows Phone 8 and it works in Android and iOS.
I’m using single domain and also I’ve used cross domain flags just in case still no luck with second ajax call. I have ajax calls in each of the JQM page, only the ajax call which is in the login page work and all other ajax calls doesn’t work though I get (HTTP 200 OK )as response, I do not see the request in Tomcat log in the server (though I can see first login ajax request).
I think that the jsession ID created during initial ajax call (login) gets lost in some way during navigation to next JQM page.
This is the ajax call I’m using in my application,
request = $.ajax({
url: "http://X.X.X.X/Servlet/Login",
type: “post”,
data: serializedData
});
I have the following flags in the app, though I’m not using a cross domain (all calls are made to same IP).
$(document).bind("mobileinit", function() {
$.support.cors=true;
$.mobile.allowCrossDomainPages = true;
$.mobile.pushStateEnabled = false;
$.mobile.touchOverflowEnabled = false;
$.mobile.defaultPageTransition = 'slide';
$.mobile.defaultDialogTransition = 'pop';
$.mobile.transitionFallbacks.slide = 'none';
$.mobile.transitionFallbacks.pop = 'none';
$.mobile.buttonMarkup.hoverDelay = 0;
$.mobile.phonegapNavigationEnabled = true;
});
I have set data-ajax=“false” in the forms I use.
Also I had to remove event.preventDefault();for Windows Phone 8 other wise after submit button is clicked the page refresh’s with no action being done.
I want to state again that, my code works perfect in Android and iOS (with just cross domain flag added). But in Windows Phone 8, I’m unable to make second ajax call to the same server.
I hope I can fix the issue with your help, any lead will be much appreciated. thanks.
this is likely related to a cache issue - to try and solve this, please try adding a miscellaneous random value in your serialized data. You can reference this
Prevent browser caching of jQuery AJAX call result
i had a similar issue with an ajax call and this random variable solved the problem
I had to enable cookies from internet explorer of Windows Phone 8 to fix my issue. Hope this helps to solve someone’s frustration.
i had the same issue. i disable the cache and fix the issue:
request = $.ajax({ url: "http://X.X.X.X/Servlet/Login",
type: 'post',
data: serializedData,
cache: false
});
I'm building an iPhone app that displays a UIWebView pointing to a web application I've created.
The web application makes frequent web service calls for data items which are used to animate controls on a canvas. The calls for data use jQuery ajax, passing parameters via JSON and receiving an XML response.
I'm finding that while user interactions with the UIWebView are occurring, the javascript setTimeout method is blocked and doesn't seem to execute at all. Fair enough; there are ways around this.
But the major problem is that every now and then after user interactions (zooming, panning etc), the ajax web service calls will just fail all the time and I can't establish a reason why. Even if they are made repeatedly, for the next few minutes none of them will even get through to the web service. If you completely leave the UIWebView alone, they will never fail as long as the web service is up and connectivity is present.
Can anyone suggest why, and how to fix/work around this?
Quick update: according to the Safari mobile debugger, the 'response' object in the error function is undefined. (It works if, for example, I make the URL invalid. This can then be called from objective-c by [webView stringByEvaluatingJavascript:#"lastError"], but throws an exception for this 'touched the uiwebview' error):
$.ajax({
type: "POST",
url: "WebService.asmx/GetValues",
async: true,
data: "{'pageVersionIndex': " + PageVersionIndex + " , 'timeStreamIndex': '" + TimeStream + "'}",
contentType: "application/json; charset=utf-8",
dataType: "xml",
success: function (response) { UpdateControls(response); },
error: function (response, status, errorthrown) {
calling = false;
lastError = response.statusText; //Throws exception
connectionInterrupted = true;
DataRoutine = window.setTimeout(DataService, dataFrequency); }
});
I'm afraid you are toasted... in a way. In iOS Safari and in UIWebView respectively system processes have priority over browser and if there is a sudden demand for more CPU power or memory for native processes (like handling touch etc) it might happen that any running javascript will be stopped from executing to reduce the memory load or cpu usage. The worst part is that it won't throw any errors or anything... it just stops your code execution as if nothing happened.
Afraid that if it happens a lot in your app the only way would be to add some kind of timer that would listen if the request wasn't blocked if so - do it again until successful.
Ups and downs of iOS - they really like you to go native rather then web :)
hope it helps,
Tom