Apologies if this question is a bit vague, but I have no idea how to make it more specific. I am a beginner when it comes to javascript. I have a web page which sends a number of ajax calls to the server to call php scripts which runs some sql against a mysql database. Everything was working fine then all of a sudden I started to get an internal server error 500 on one of my ajax calls. The ajax call is placed in a function, it works most of the time. Only when I pass the function one specific sql query does it give the error. I've tested the query in my database manually and it works so its not an issue with the query that I am passing. The strange thing is, since the error has started, every now and then it does work, however most of the time it is not working, the error seems to be somewhat random. I realise that with limited information it may be impossible to give an exact answer, but has anyone experienced anything like this before? OR does anyone have any clues as to how I can get to the bottom of this. I've tried everything. I will paste the function that is causing the error (not sure if that will help at all). The error comes on the "xmlhttp.send();" line.
function phpRequest2(prov, phpsc, funct, bolL) {
<!-- document.getElementById("demo").innerHTML = qry; -->
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
document.getElementById("demo").innerHTML = prov;
xmlhttp.open("GET", phpsc + ".php?qry=" + prov, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var arrLocs = JSON.parse(xmlhttp.responseText);
funct(arrLocs, bolL);
}
}
}
You best bet at finding out the reason when you get the Error 500 is to edit your php.ini file and change the following parameters
display_errors = Off
display_startup_errors = Off
to
display_errors = On
display_startup_errors = On
Hopefully that should give you exact details on what your error is and thereby help you solve it.
Related
I'm trying to execute a block of code when my XMLHttpRequest reaches state 2. The reason why I want it to be in state 2 is that I don't want the user to wait for a response of the server( I would like to redirect the user at this point).
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 2) {
window.location.href = urlFromPreviousAjaxcall;
}
}
xmlhttp.open("POST", "url", true);
xmlhttp.send();
However the block of code inside the if(xmlhttp.readyState == 2) will only be called as soon as the server is done processing the call. This part has to be executed as soon as the call is made( without the waiting from the server).
In the documentation I found that state 2 is reached as soon as the call is send. However that is not the case.
Update:
the call I am trying to make involves calling a Api on the server( this takes time to complete). For the client it doesn't matter what happens to the call.The only thing I want is that the call is executed. So basicly I'm trying to gain speed here.
I know that as soon as I redirect the user, the code will stop running. However the call to the server should have been made(and send away).
What am I missing or doing wrong?
Thanks you all for your help.
I found the solution:
[HttpPost]
public void methode(String parameter,String parameter)
{
Task.Factory.StartNew<string>(() => RunTask(accessToken, parameter, parameter));
}
private string RunTask(String parameter, String parameter)
{
try
{
// Code to execute here
return "Done!";
}
catch (Exception e)
{
return "Error: " + e.Message;
}
}
At the server I started a Task. In this task I execute the long procces. It still takes a brief moment before the server returns the call to the user.
When debuggin ( in visual studio) you can see that the task is running without the presents of the user.
Note : all sessions are gone at the moment I redirected the user to an other page.
I am new to javascripts
please help me to solve this,
I have a function for db trip. that fires on every 10 mili seconds. problem is data is varying some time it gives 3 records sometime 5 records. there is not an issue of sp parameters I have passed. I think it's due to the function call which is not a thread. so process of function call is overlapping before the previous call complete
I have seen article for multi threading
https://gist.github.com/johdax/1269740
but don't have idea to integrate my function with threading.
this is my function
<script>
setInterval(function(){UserList()},10);
function UserList()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp6=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp6=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp6.onreadystatechange=function()
{
if (xmlhttp6.readyState==4 && xmlhttp6.status==200)
{
$("#UserStatusList").html(xmlhttp6.responseText);
}
}
var a = $('#cmbProjectList').val();
if (a==null){ a=""}
xmlhttp6.open("GET","UserList.asp?ProjectId=" + a,true);
xmlhttp6.send();
return false;
}
please help me to solve this.
how can I apply threading on this?
Browsers don't really use threads in any exposed way, AFAIK. If you wanted something truly off the normal browser flow, you could look into the Web Worker API which makes a separate sandbox for JS activities.
This question has been asked twice on these forums, but the answer provided is not working for me.
The issue is that I have JSP page which is returning and flushing small amounts of output.
I am using the following code to read the output:
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 3) {
alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST", "download.jsp", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader('X-Requested-With', "XMLHttpRequest");
xmlhttp.send($('#submitDownloadForm').serialize());
On Firefox this works fine, and I am given 3 alerts during the duration of the process.
However, on Webkit based browsers such as Chrome and Safari, I am given the first alert, but not the other 2 until the process has completed.
Other answers have said that changing the Content-Type:text/plain or Content-Type:application/octet-stream, but if I do this, the readyState jumps straight to 4 as if the process has completed instantly.
I cannot find any solutions for this.
Any help greatly appreciated, thanks in advance everyone.
I added the following code to the JSP file just before where it produces the output, and this has resolved the issue on Webkit-based browsers:
response.setContentType("application/octet-stream");
I'm now getting the updates every time output is flushed.
Ok, I know this has been asked before (as I have viewed it myself) and it seems to work for everyone else except for me. Im terribly new to javascript, as I try not to use it cause its a nightmare to debug. But, here is my issue. I have a script that im working on for ajax to add an image to a gallery. The image is just an id that references another table in the database, and same for the gallery. Just two id's. Anyways, when I click on a link, I want it to run this script to add it to the database. But, its not working, and of course, javascript is horrible at letting you know where it fails. Here is the code for the script.
function addToGallery(img)
{
var e = document.getElementById("galleries2");
var gal = e.options(e.selectedIndex).value;
window.alert("Gallery Id: "+gal+" Image Id: "+img);
if (img=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","addtogallery.php?img="+img+"gal="+gal,true);
xmlhttp.send();
}
Ok, so the above doesnt work. I added the alert message to try to troubleshoot as much as I could. If I put the alert box above the var's, and only ask it to reference the img variable, it works. Now, if I put it below and try to do what I did there, it just stops. No message box, no ajax, nothing. So, im betting it has something to do with the
var gal = e.options(e.selectedIndex).value;
I have also tried it with the [] instead of the () and nothing there either...
Any help would be appreciated.
UPDATE
Ok, so I have got past the problem with the select, now im trying to pass values with the xhtmlrequest.open method. I can pass 1 get value it seems, but not two. Here is the line of code in question
xmlhttp.open("GET","addtogallery.php?image="+img+"&gid="+gal,true);
Now, I know that img and gal are set because of an alert box that pops whenever this script is run to tell me that they are set. But when it gets to the php page its only putting out the img variable, and the gal variable is still not set. Anyone have this issue before?
You definitely need square brackets, but also try changing your variable "e" to something else like "el". The function responds to a click as I understand, so it might be reserved for an event.
Try:
var galleries = document.getElementById("galleries2");
var gal = galleries.options[galleries.selectedIndex].value;
I think what you want to do is:
var gal = e.options[e.selectedIndex].value;
Hope this helps.
You need square brackets instead of parens.
var gal = e.options[e.selectedIndex].value;
Your JavaScript environment should be giving you a nice TypeError saying something like "HTMLOptionsCollection is not a function"
I'm trying to do a simple AJAX call to an ASP page, that resets session variables and posts a little message back on completion. I'm doing this purely to learn AJAX.
The example came from W3 Schools website but since applying it to my page, I can't seem to get it to work and it's not producing any errors, which is annoying, because I can't debug it.
This is my JS, which is called when a user hits a button [Clear Form]:
function resetSearchForm()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("notification").innerHTML=xmlhttp.responseText;
document.getElementById('notification').style.visibility = 'visible';
}
}
xmlhttp.open("GET","clearSearchData.asp",true);
xmlhttp.send();
document.searchFrm.searchStr.value='';
document.searchFrm.vertical.checked = true;
document.searchFrm.horizontal.checked = true;
document.getElementById('dateRange').selectedIndex = 0;
document.searchFrm.searchStr.focus();
}
And this is the ASP (clearSearchData.asp) that clears my session variables and writes a message:
Response.Expires = -1
Session("search-str-boolean") = ""
Session("search-str-plain") = ""
Session("date-range") = ""
Session("date-from") = ""
Session("date-to") = ""
Session("specificDate") = ""
Session("peopleStr") = ""
Session("orientation") = ""
Response.Write "Form has been reset"
Can anybody see where I'm going wrong? I have been looking at it for a long time and I just can't see it.
The function itself works because the last part of the function gets processed, the bit that clears the form values... but... the AJAX call doesn't happen because the session variables still contain data and the message doesn't appear.
Many thanks in advance...
UPDATE - - - - - - - - - - -
It now works. The problem was I didn't include the full URL to the ASP page. Thanks for 'thedaian' (below) for pointing that out
Chances are, something is wrong with the page you're trying to get via AJAX. Check what xmlhttp.status is, if it's 404, then you're never going to get to the point where you're printing the AJAX response. Make sure that "clearSearchData.asp" is accessible from the same directory as your javascript. This is a common problem if you have your javascript code in a separate folder from the rest of your site. Or simply put in the full URL path for the "clearSearchData.asp" so it'll definitely work.
Something to point out, the function in xmlhttp.onreadystatechange is (usually) called after it's declared in the code. In this case, it gets called after your search form fields are cleared out and reset.
The ajax call does not automatically send along a session cookie. That means the session you're clearing is not the user's session, but just a session that's been created for that ajax call alone.