Some of our customers are reporting that they are unable to execute some javascript actions. Our error data collecting is on the server and when javascript breaks we are unable to get notified it since ajax calls can't be run before page refresh.
Is there a way to collect javascript errors on client side and send them to the server as soon as javascript is running fine again.
Example:
User goes to page a.php.
There's faulty javascript on a.php and it breaks user's javascript.
User's client logs the errors and stores it locally.
User goes to page b.php.
There are no faulty javascript on b.php and the client detects that there's javascript error logs stored locally and proceeds to send them to the server.
The server writes the log up.
There is no need to refresh the page to make AJAX calls. You just need to make sure that the error handling code is in its own script tag so that it won't break if there is a syntax error in the other code.
Here is part of the code that we are using to catch script errors and send them to the server (The AJAX code is in a separate method, but I trust that you already know how to do that):
window.onerror = function (message, url, line, column) {
Ajax.logError(message, url, line, column);
$('body').append($('<div>').addClass('ErrorPanel').text('An error occured in the browser.').delay(5000).animate({ height: 0 }, 2000, function() { $(this).remove(); }));
return false;
};
Maybe you should take a look at New Relic. New Relic is a monitoring tool for software analysis and it's able to show you various information about JavaScript errors (and much more):
http://newrelic.com/browser-monitoring
Related
insert data into database using javascript in asp.net C# After inserting the data into database,i want to create the alert box
Try using RegisterStartupScript instead. Also, side note, you have a typo in your alert message box. exits != exists :)
ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alert('employee Already Exists !.')", true);
You can't use register client script inside of a web method, since there is no round trip and page cycle occurring.
Register client script requires that the page was posted to the server, then code behind runs, and then the WHOLE web page travels back to the client, is re-loaded, then displayed, and THEN your injected script runs.
however, since this is a web method, then that means client side js code is calling and running the web method, and thus that means the client side script can launch/display/have/enjoy code to launch/display any dialog box or whatever you want to display in that client side js code. So, have the web method return some "yes text" or whatever you want to display, and write that code in the client side js code to then display the correct message or dialog based on what the web method returns.
So, to "inject" or "register" some js code into the web page? That requires that the web page has been posted to the server, and that means a post-back of the web page will have to occur.
The web method can't update controls on the web page, nor inject js scripts, since the page is STILL sitting on the user's desktop, and never made the trip to the server.
however, as noted, since your js code client side will be calling the webmethod, then it should be a simple matter to have such js code client side display that message based on a return value from the webmethod. In your case, you return "true" as a string if the data was to be inserted, and thus in place of a register script, return a "false" string, and have the calling js code then display some message based on that true or false returned to that calling js code.
I was wondering if there was any way to create something like a txt file in Ajax. I'm using this to save logs created by the user on my site, this is the link if its helpful: site. If this cannot be done with ajax can it be done with cookies? Or would the log be to big for cookies? Thanks(sorry if this is a duplicate, some I looked around and everything was about loading/reading from a file).
AJAX is just making asynchronus HTTP calls on the browser.
So, can you create a file just making an ajax call? No. You would need to setup somekind of script on the server that will handle the file creation and writing and call that script with AJAX.
The short answer is - no. You can not do this DIRECTLY with AJAX.
The longer answer is - what you are looking for should be done using a database and SQL. Not that PHP couldn't do it directly - but a good database is better able to handle this problem.
AJAX is mainly used to just send and receive messages, files, etc... It isn't for storing anything. With that being said, the steps would be to have a small PHP script that just reads and writes to a database and make that database be a log file. So your SQL database (I'd just call it log) would have two entries. These are the date the log entry went into it and the entry itself. You could also throw in last_accessed if you want or other fields you think you might need. Your PHP script, in its simplest form, would open the database and, depending upon whether you send it a "GET" or "PUT" command it would read from or write to the log file. If it writes to the log file, it sends back an ACK or NAK (meaning everything is ok or the program had a problem). If it is reading from the log file, you supply how many lines you want to read and the PHP script either sends back the lines or a NAK again to signify something went wrong. You can also replace the simple NAK with the actual error if you want but then your customers are seeing the error. Better to have just a generic "There has been a problem, we have contacted our system people and the site should be back up shortly" kind of message. Save the actual errors for you to see via e-mail.
That's it. You should be able to write this in about ten to fifteen minutes and have your log file readily available to you in no time.
Alright, I have been trying to work on this issue for days, and have found no fix. I've asked about 4-5 different questions pertaining to my issue, but have found no solution.
file.php on example1.com:
I have a JavaScript function on this page that sends a POST request to a PHP file on example2.com (a DIFFERENT domain).
function buttonFunction() {
$.post("http://example2.com/core/runner.php",{username:username, password:pword, coins:coins}, function(data) {
// Stuff
});
}
This function dynamically loads the result of runner.php into a div on the page.
Now if the user leaves the page in the middle of execution (the result hasn't been generated yet) and then he/she refreshes the page, and then decides to run the function again, then two PHP proccesses would be running from the same user at the same time (that is, if the old one was still running).
Now I need a fix, whether that be in the PHP file on example2.com or within the JavaScript on example1.com that can abort/stop the previous PHP request before starting a new one.
What I've Tried:
#1
I've tried this, but unfortunately cross-domain cookies is practically impossible:
PHP session files saved, but no cookies and session not read
The goal was to store the PHP pid in a SESSION variable, and on every run, to check if the old proccess with that pid was still running (by grabbing the pid from the SESSION variable), if so, then I would kill that processes and change the value of the SESSION variable to the new pid.
#2
I've also tried to run a loop to check if the connection has been aborted while the main script runs. However, that also did not work:
PHP run loop and script at same time
#3
I also thought about doing something like this:
window.onbeforeunload = function(event) {
http.abort();
};
So it would abort the request before the user left/refreshed the page/browser. However, I was unsure about the reliability of this (Safe Way to Send POST Request via Javascript).
As pointed up by the other user, using files are the best way for doing this. My logic would be something like this:
When the runner.php runs:
First check if a file named username+number exists
If exists then delete that file, and create a new file named username + (number+1)
If does not exist then create a file named username+1
Store this number (i.e. 1 or number+1) in a variable
Do whatever processing is required
Before sending out the response back, check if the file named (username+ your number in var) exists
If yes, send out the response
If no, it means that another (later) process is running (only for that user because we name the file with username) and that process has deleted your current process's file, so just terminate without doing anything.
Of course this might not be the optimal solution, but should work for your use case.
Check the file functions in PHP, there are no complicated magic code required for this.
When You execute Your script runner.php for the first time You can create a file and lock it until script ends, so when there is another request to that script You can check if file is locked - and then You have information if You can run script again, example:
$fo = fopen('lock.txt', 'r+');
if (flock($fo, LOCK_EX))
{
// File was not locked, You have locked it successfully right now, do your logic
flock($fo, LOCK_UN); // unlock file afer script logic
}
else
{
// File is locked right now, don't run script
}
After that You can send message to user that he can try later (cause first script is running) instead of killing first script.
I think this is design problem when it comes to killing script, it doesn't look well. What about data when You kill script in middle of its execution? This can lead to some serious issues.
I'm currently creating an image hosting script and so far so good. I've used several plugins to create the local uploading process with drag & drog + AJAX which works totally fine. Now I've moved to the part where I need to create the remote uploading process with jQuery AJAX and a PHP script to handle the whole thing.
How it's gonna work
My thought are like this: There is a big box in the middle of the page that accepts the URLs to be remote uploaded. Once valid URL(s) are passed into the text area, they will be immediately sent to the server side script via jQuery AJAX. It's bound with a keyup event.
This is how it looks like: http://i.imgur.com/NhkLKii.png.
The "HERE COME THE URLS" part is already a text area - So that part's already done.
Where I need help
The issue with this whole situation is: Once there are valid URLs pasted into the text area, those must be immediately be converted to some sort of box which also includes an uploading progress. Something that looks like this (copied from the local uploading part): http://i.imgur.com/q7RyDmb.png
It was easy implement the progress indicator for the local uploading, since it was a feature offered by the plugin I've used, but I don't know how to indicate the progress of remote uploading, which is totally being made from scratch.
So this is how I've imagined the logic to flow:
User pastes some URLs into the text area
There is a client-side check to validate the pasted URLs
Validated URLs are send to upload.php on keyup (?)
URLs are being processed
While the upload goes on, we show the users the progress in the knob (?)
PHP script finishes the process and returns back the uploaded URLs
I update the page in the AJAX success callback to display the uploaded files
So, the two process flows marked with (?) are unclear to me - I don't know how to achieve those...
What I have tried
Well, I didn't just come here and ask you to do everything for me, but I've come across a dead end and I don't know how to continue. What I've done so far is collect the URLs from the text area, and if there are multiple URLs separated by a line break (\n), I simply use split to get an array of pasted text and then use another function inside the loop to validate if they are URLs. If there is no line break detected inside the text area value, then I simply check the one line that was provided. On each case, I send the whole text area to the PHP script, because I don't know how to get rid of the invalid URLs in jQuery. I've created a function called debug() in PHP which stores anything into a debug.log file and this is what I'm getting (in one try) when I paste something into the text area:
https://www.google.com/https://www.google.com/
I paste https://www.google.com/ once in the text area, but it gets logged twice in the PHP side and I can't determine why.
This is how my jQuery looks like:
// Remote upload
var char_start = 10;
var index = 0;
var urls = $('.remote-area');
var val_ary = [];
urls.keyup(function(){
if (urls.val().length >= char_start)
{
var has_lbrs = /\r|\n/i.test(urls.val());
val_ary = urls.val().split('\n');
if (has_lbrs)
{
for (var i = 0; i < val_ary.length; i++)
{
if (!validate_url(val_ary[i]))
{
val_ary.splice(i, 1);
continue;
}
}
$.ajax({
type: 'POST',
url: 'upload.php',
data: {
upload_type: 'remote', // Used to determine the upload type in PHP
urls: val_ary, // Sending the whole array here
},
});
}
else
{
if (!validate_url(urls.val()))
{
// Display an error here
return;
}
$.ajax({
type: 'POST',
url: 'upload.php',
data: {
upload_type: 'remote', // Used to determine the upload type in PHP
urls: urls.val(), // Sending what's in the text area
},
});
}
}
});
The questions
So the final questions are:
How do I send my information correctly to the PHP script, only valid URLs and have them kind of "process-able" in my PHP script.
How do I indicate the progress of the upload?
If I was somewhere unclear during my question, please let me know, I'll try to reexplain.
Thank you.
Updates
09/12/2013
I think I have managed to solve the double-sending issue where my AJAX would send the same information twice to the PHP script. What I did was code in a delay anonymous function that sends the text area content to the PHP script after an user stops typing for 2 seconds. Once the user stops typing again, the timer resets and a new AJAX request will be made. So, I'm assuming that this issue has been solved. I'll come back to it if anything strange occurs.
Now I'm still left with the progress indicators part. I'd appreciate your thoughts on that one.
My new code: http://pastebin.com/SaFSLeE9
What you're looking for in terms of communicating progress back and forth is "pushing". That refers to the technique of server sending data to the client, rather than the other way around, which is the standard HTTP way of doing things.
You've got plenty of options available, as described in the explanatory Wikipedia article, though perhaps more relevant to this topic would be Comet. What happens is you trigger and $.ajax call just like the one you have now, but you set a very long timeout. That essentially gives the server a "channel" to send data back to the page whenever it's available.
So what you need is a .php on the server that is capable of handling long polling and will send data back to the page as the upload progress changes (probably in array form for multiple uploads). This article should get you started with some jQuery code. Just remember that this request doesn't go to upload.php. This request goes to a different script that deals solely with upload percentages and only returns data when that is available, it doesn't return immediately as all others scripts - the Ajax will happily wait for the data.
Also, don't separate your code like that with has_lbrs. One line or many are not distinct cases, one line is just an edge case of many lines. You're duplicating the code unnecessarily. What does the else case do that would break in the general case? Further, the "error handling" in the else case is misleading. The only error reporting you do is if there is only one line and it's wrong. What if you have two lines and they're both wrong? Your code will happily send an empty array to upload.php.
This is why I think you shouldn't separate your code like that, because then you'll split logic and not even notice it.
In my opinoin, the best way is to call your cURL script with ajax and use it to upload your files on remote server. You need ajax.js, curl.php, index.php (whatever name you want) on your app server. And image.php, class.image.php (whatever name you want) on your remote server.
Steps that I did for my app
1) I am going to upload an image from my index.php file. It will call curl.php file using ajax.js and the cURL file will check file's extension and all (for your app's security, make sure what you want to allow users to upload).
2) Now the curl file will upload the file to your pre defined temporary folder with the default file name.
3) Now if move_uploaded_file function (which I used in my script) run successfully, you can call your cURL function to send your data as post on your remote server, where image file will receive posts and will process further. You can keep your class in image.php or you can create two PHP files on your remote server, as you want.
4) Now in your class file, you should check file once again that it is image file (and whatever you want to allow) or not for better security. If file is good, process to rename it and add file into folder if you want to.
5) Add file's new name and folder name into your database by using remote database connection. So, cURL will show you result on the same page.
Now, why cURL? I prefer cURL because, you can add secret key or API for your communication to make it more secure, with if else conditions. Your remote server file which is going to receive all posts, will check if API == 'yourKey' then will process other wise it wont process and nobody will be able to send images on your server with bots and all.
I don't know that my answer is going to help you or not, probably my method is lengthy or not good for your app, but try to Google about cURL and you will understand what I am trying to say. Hope you like it and understood it. If any doubt, you can ask me any time.
I have an application in which most requests are submitted via AJAX, though some are submitted via "regular" HTTP requests. If a request is submitted and the user's session has timed out, the following JSON is returned:
{"authentication":"required"}
The JavaScript function which submits all AJAX requests handles this response by showing a popup message and redirecting the user back to the login page.
However, when a non-AJAX request receives this response the JSON is simply shown in the browser because the response is processed directly by the browser (i.e. the aforementioned JavaScript function is bypassed). Obviously this is not ideal and I would like the non-AJAX requests that receive this response to behave the same as the AJAX requests. In order to achieve this, I can think of 2 options:
Go through the application and convert all the requests to AJAX requests. This would work, but could also take a long time!
The JSON shown above is generated by a very simple JSP. I'm wondering if it might be possible to add a JavaScript event handler to this JSP which is run just before the content is displayed in the browser - I'm assuming this would never be called for AJAX requests? This handler could call the other JavaScript code that displays the popup and performs the redirection.
If anyone knows how exactly I can implement the handler I've outlined in (2), or has any other potential solutions, I'd be very grateful if they'd pass them on.
Cheers,
Don
3) Change your AJAX code to add a variable to the GET or POST: outputJson=1
You cannot add a handler to the JSP that way. Anything you add to it will make it a non-JSON producing page.
There are two options that I can see:
Add a parameter to the page by appending a URL parameter to the screen that modifies the output.
URL: http://domain/page.jsp?ajaxRequest=true
would output json only
URL: http://domain/page.jsp
would display a jsp page that could forward to another page.
OR
change the response to have the forwarding code in the JSP that will get executed by the web browser if it is hit directly. Then have your calling AJAX to strip the forwarding code out, and then process what is left.
4) Read up on the 'Accept' request HTTP header.
Then, on the server side tailor the output:
e.g.
if(Accept contains application/json...) { // client asking for json, likely to be XHR
return {"foo":"bar"}
} else { // other
return "Location: /login-please";
}
Start with a smarter error message, like this:
{"error":"authentication required"}
Wrap the JSON output in a callback:
errorHandler({"error":"authentication required"});
Have a handler waiting in your script:
function errorHandler(r) {
alert(r.error);
}
And don't forget to send it down as text/javascript and not application/x-json.