Unable to consume WCF service by Java Script using XmlHttpRequest - javascript

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.

Related

google sites JS HTTP request

I have a remote server that receives GET requests with parameter and returns a string.
I want to add a JS code in a google site page that will HTTP GET to that server and print the response string in a table, using the HTML box with:
function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true);
xmlHttp.send(null);
}
Google sites does not approve it and rejects the function all together.
What other options do I have?

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.

XMLHttpRequest - Hitting serverside aspx method - 505 Internal Server Error

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

Consuming POST method of WCF REST service in Javascript or XmlHttpRequest

I am trying to access a WCF REST service with GET Method. The problem with this is, my parameter at times could be very long (more characters greater than maximum characters of URL String), So I am thinking to implement the same functionality with POST.
Note: I am consuming the service through JavaScript (I don't want to use JQuery - Since I am already using using Sencha (Sencha doesn't provide any POST method)) XmlHttpRequest.
function GetData() {
// alert("hello");
var xmlhttp = new XMLHttpRequest();
var url = "http://lclhost.com/WcfWebService.svc/doworks";
// alert("hello1");
xmlhttp.open("POST", url, true);
/// alert("hello2");
var params = "name=Jack";
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function () { //Call a function when the state changes.
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
else {
alert("Status:"+xmlhttp.status+"State"+xmlhttp.readyState);
}
}
xmlhttp.onerror = function (e) {
//alert error
alert('error');
}
xmlhttp.send(params);
}
and my service is
[OperationContract]
[WebInvoke(Method="POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "doworks")]
string DoWorks(string name);
While consuming the javascript function through asp.Net web application, I am getting an alert of Status:0State4. I am not sure what is the problem. Can you guys help me how to consume this Service in POST through XMLHttpRequest/Javascript?

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.

Categories