XMLHttpRequest - Hitting serverside aspx method - 505 Internal Server Error - javascript

I have coded an AJAX GET request using a XMLHttpRequest object. My problem is that the request is never hitting the server side code. I had a look at many forums and at stackoverflow and I dont really know what I am missing.
My JS code
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var response = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "login.aspx/GetData", true);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(null);
}
Server side code - login.aspx
[WebMethod]
public static string GetData()
{
return "break here";
}
Looking at the Network tab in developer tools, I see that it cannot find the method as the error code is 505 (Internal Server Error)
Can this work within an aspx page? maybe only wrks in a asmx service?
Is there anything in the code that I have not done or is wrong?
Thanks

Related

Why is my firefox extension only posting data when on localhost and not on any other websites?

I have a Spring server and am using xhttp to post data to the server from a firefox extension. The data will only send from localhost and not from any other websites.
'''javascript
function sendo(keys) {
try{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhttp.open("POST", "http://localhost:8080/index", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.setRequestHeader("X-Data", keys);
xhttp.send("{\"name\": \"Urmi\"}");
console.log("Sent " + keys);
}
catch(e){
console.log(e);
}
}
'''
When this function is called while on localhost, everything executes correctly and the keys are correctly posted to the server. When on any other website, the code seemingly runs, as the console.log at the end of the try statement runs, but nothing is ever posted to the server.

How to import an xml file with javascript

So i've been googling this for a while now and no solution seems to work for me.
Given that the url to the xml file (taken from valve api) is: playerSummariesXml
I tried ajax calls such as:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
alert(xhttp);
}
xhttp.open("GET", playerSummariesXml, true);
xhttp.send();
}
Which returns the link to my website with a /0 appended to the end,
and
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
which says activexObject not declared
And other things...
Is there one resolute way to pull an xml file with javascript from a given url and read/display it?
I'm getting really confused considering this is super easy to do with php and don't see why I can't find a similar thing with javascript.
Can you try the below code,
var xhttp;
if(window.XMLHttpRequest){
xhttp = new XMLHttpRequest();
}else {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
alert(xhttp);
}
}
xhttp.open("GET", playerSummaries.xml, true);
xhttp.send();
1: you need to check if browser supports XMLHttpRequest or ActiveXObject Object.
2: .open() and .send() should not be written inside onreadystatechange function.
3: check if your referencing your xml file correctly, playerSummaries.xml or playerSummariesXml.
You can see my below code on GIT hub , which uses plain JS ajax to support for html,css,json ,xml,image responses.
https://github.com/vijaysani/JavascriptAjaxWithDifferentResponses
let me know if your still facing any issues.

Ajax request to load .php file not working

Trying to load contents from postcode.php file into a #postcodeList div, but it is not working (nothing happens). I checked postcode.php file it echoes al correct information.
var loadicecream = document.getElementById("iceCreams");
loadicecream.addEventListener("click", iceAjax, false);
function iceAjax() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","ice-cream-list.php",true);
xmlhttp.send();
document.getElementById("ice-cream-list").innerHTML = xmlhttp.responseText;
}
You want the query to execute asynchronously (the third parameter to open function) and then you synchronously try to read the value. This happens before the query has been sent, so it will always be empty.
Either run the load synchronously, or set the xmlhttp.onreadystatechange to point into a function where you handle the loaded state. The best way is to do it asynchronously since you don't want to block the user from using the page while loading data.
Quick example, only handles the success case:
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("postcodeList").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST","postcode.php",true);
xmlhttp.send();
Read up on the documentation for the onreadystatechange, at least you want to handle the case where there is a timeout or some error, otherwise the user won't know something went wrong.

AJAX: head and more

For asynchronous quick-checks of an URL, I use AJAX with method=HEAD:
function ajax_quickresponse(url) {
var xmlhttp = null;
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject)
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
if (xmlhttp) {
xmlhttp.open("HEAD", url, true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if ((xmlhttp.status > 199 && xmlhttp.status < 400))
; //OK
else
; //ERR
};
};
xmlhttp.send(null);
};
};
This receives is enough for checking the http status, but seems to abort the script at serverside (url). E.g. I can try with a simple php script (url="http://my-host.er/test/script.php") which does sleep(2); and log a success message afterwards.
With xmlhttp.open("HEAD", url, true);, there is no success entry in the log.
With xmlhttp.open("GET", url, true);, there is a success entry in the log.
However, with GET/POST, the javascript is waiting 2 seconds instead of instantly (with HEAD).
The status is known instantly and the javascript does not need to wait for the final response.
How to take advantage of both methods? First, I need the status instantly, as soon as the header comes in, and after the external url/script returns, then i'd like another listener.
E.g. first alert('http status='+xmlhttp.status); and maybe delayed, depending on the url/script, alert('finally completed');
Do you have a tip how to achieve this with one single call?

Unable to consume WCF service by Java Script using XmlHttpRequest

Here is my java script to call WCF service
function CallWcfAjax() {
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:20949/RestService/tiger/pandey";
// Send the HTTP request
xmlHttp.open("GET", url, true);
xmlhttp.send();
// Create result handler
xmlHttp.onreadystatechange = function X() {
if (xmlHttp.readyState == 4) {
alert(xmlHttp.responseText);
}
}
}
if I enter the same URL in browser http://localhost:20949/RestService/tiger/pandey i get a response.
Any idea what wrong I am doing in Java Script?
Thanks
You variable name is xmlhttp and in the rest of you code, you use xmlHttp. This is a problem.

Categories