Showing posts with label xmlhttp. Show all posts
Showing posts with label xmlhttp. Show all posts

Wednesday, March 28, 2012

Why XmlHTTP?

Have we looked at standard DHTML alternatives to XmlHTTP? I've never used it in my AJAX apps because of browser compatibility and security dialogs cross-domain. I understand using it to connect to non-Atlas web services, but an Atlas client to Atlas web service could asychronously request data post-page load by requesting script - e.g. I could call the web service like "myservice.asmx/js?param1=foo¶m2=bar".

You are right that XmlHTTP can have browser compatibility issues, which is why a client framework like Atlas is useful. When you use Atlas, you never directly use XmlHTTP. Instead, you use Atlas abstractions built on top of it that have the necessary logic to make it work in most browsers.
I'm not sure I understand the alternative that you're suggesting. Another method that is sometimes used it to make the request into a (hidden) iframe, and get the data from there. Is that what you are referring to? Note that doing this will hit the same cross-domain limitations as using XmlHTTP (this is by design for security reasons).
David

Thanks, I understand the user doesn't use XmlHTTP directly. But the underlying layers use it exclusively (at least from my cursory examination of AtlasCore.js), limiting the browser support.
Yes, that is the technique I am referring to. It does not have cross-domain limititations (there's no reason I can't include a script from another domain - by placing the reference in my page, I am allowing that script to run in my domain). If you're interested I can point you to a live demo application on one domain requesting data from another domain (asynchronously, post-page load, no-xmlhttp).
Jim


Hi Jim,
If a page has an iframe, it can navigate that iframe to any URL. However, it cannot access the data inside that frame unless it lives in the same domain, due to cross frame scripting restrictions:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/om/xframe_scripting_security.asp
But maybe your are talking about something different?
thanks,
David
Does XmlHTTP restrict us to REST-style interactions with all the pre-knowlege that requires or can we use XmlHTTP for compositional discoverable SOAP? If so, I can easily imagine AJAX style pages that will need to pull data from many many sites. Imagine, for example, an enterprise window to embedded control systems accessible by SOAP such as available from
BACnet under BACnet-XML
LON using iLON 100
Tridium using the new Niagra framework
In each of these cases you are discussing a single interface pointing to multiple exposed data sources. Lets move the bar higher, then, to also pull vector XML (say SVG) from some sort of drawing room
If I have all these sources in my trusted sites, why shouldn't I be able to cross-script across them?
Hello.
I'm a much interesting in your live demo. If it possible can you send it to me at mrorbit [at] pochta.ru.
Thank you in advance.
Another method I have used is loading a script with an IE:DOWNLOAD-control and call it.
http://searchpalette.com/g/g.html
This method will work with different site, I think. Also, I believe it works on a few browsers. To incorporate this int the ASP-world might not be worth it though.
If there is any interest, I could give it a try.
Gorm Braarvig

Wednesday, March 21, 2012

writing json back to client

I am making an xmlhttp request (using the scripts provided inAjax for Web Application Developersby Kris Hadlock) to get data from an aspx page. When I create a dataset and return xml with an xmldocument it works fine. I am using the following where dsreturn is the getXML of the dataset.

Dim xdocAs XmlDocument =New XmlDocument()

xdoc.LoadXml(dsreturn)

xdoc.Save(Response.OutputStream)

The problem is that I also want to return json. I have created a biz class to parse a dataset and return a json string. I know this works because I have used it in other web services. When I do this I get nothing. I think that my problem is how to return the json string in the response.outputstream. I am not sure how to do this. Just using response.write doesnt seem to work.

Thanx

Have you considered using page methods instead of doing it all by hand? They return JSON by default.

xmlHttp ajax issues

Hi all, totally baffled by this one!

I've got a bit of code:

xmlHttp.onreadystatechange=caravanSearchStateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
That fetches some html that's just been printed by an ASP page, and prints it to a div.

There's some code afterwards:

if (document.getElementById('hideHeaders').value == '1') {document.getElementById('resultsHeader').className = ' none';}else {document.getElementById('resultsHeader').className = 'floatleft paddingtop15';}
That checks a hidden HTML field for a 1 or a 0. If it's a 1, it hides a
div. If it's a 0, it shows a div. By default in the html, the field is
set to 0.

Trouble is, the if statement is delayed. If I want to hide that div, I
have to wait for the condition to get a 1 twice. Vice-versa for the 0.

However, if I put an alert saying "hello world!" between the two code snippets I gave you, it works!

It's almost as though the HTML is being written too late for the javascript to validate the if statement.

Trouble is, if I put a sleep function I found between those bits of code, it doesn't work.

What the hell is the problem here?

Any help's appreciated because I'm stuck!

Cheers,

Karl.

Hi,

The same problem is discussed athttp://forums.asp.net/p/1134520/1813795.aspx#1813795 &http://forums.asp.net/p/1134520/1813803.aspx#1813803

If yourHTML fieldis still not be shown,it is noneffective action at once to set the value of thefield.

The similar code in that post:

<script language="javascript" type="text/javascript">
// <!CDATA[

function Button1_onclick() {
// This works

document.getElementById('div1').style.display = "block";
document.getElementById('div1').scrollLeft = 100;


// But this doesn't work

//document.getElementById('div1').scrollLeft = 100;
//document.getElementById('div1').style.display = "block";
}

// ]]>
</script>

Best Regards


Thanks for taking a look at that for me; I've since found a solution and have written a small blog about it so that people are aware.

If you have any questions, reply to this post and I'll have a go at answering them.Smile

http://codingrage.blogspot.com/2007/09/xmlhttprequest-object.html

Cheers,

Karl.

xmlhttp and outside servers.

I'm sure this is some kind of security feature but I'm hoping there is a fix or work around.

I have a page that is doing a xmlhttp GET to an outside server to get some data back. This works 100% when I am in debug mode in vs2008. Soon as I move the application to 1 of 2 hosting sites, it no longer works. I just get "access denied"

Why does this work just fine locally but not else where? Is there a way to around this some how?

For cross server requests, you'll need to either have an external service that supports JSONP (which is honestly kind of a pain to deal with) or create a proxy service on your local server that acts as a middle man.


but why does it work on my local server, which is a basic IIS setup and not on another server?


Different browser security settings for that local server probably.


:(

Guess I'm out of luck. I'll just run the part that needs to do the request from my local machine and everything else from the hosting.

Unless anyone has any tricks up their sleeve that can make this work :)

Thanks again ASP.Net forum people


You're not out of luck.

You just need to create a web service proxy that's running on your local domain. Basically an asmx in your solution that basically does no more than call the remote service and then return that result. So, your client side functionality can call the remote service by calling the local one, and there are no cross domain issues.

XMLHTTP and Digest Authentication

Hi, I'm having some problems with XMLHTTP.open and digest authentication.


I am embedding the user name and password into the XMLHTTP.open function (method, file, async, "UserName", "Password"). The server is Apache. Basically, when I switch to Basic authentication, the script works fine. However, it does not work correctly when I switch authentication to Digest. HTTP status returns an odd number, (5 digits). And it looks like the callback function is never executed.

Anyone have any insights on this?

Hi,

It seems that the target page might have some errors.

Need more help on XML Http post, below link might help you..

http://www.devx.com/DevX/Tip/17500

Happy Coding

Please mark as "Answer" if you find this post helpful


1) The target page just spits out text. There's no scripting of any kind on the target page. The only sort of server action is .htaccess for authentication.

2) I'm not using POST. I'm using GET. However, that's irrelevant. The error exists either way.


Hi,

It's clear to us in helping if you post all your javascript and target page code.

xmlHttp with responseXML

I'm trying to write a script page that uses the xmlHTTP to an aspx page that then sends back some XML which I can then use the javascript to read and populate a dropdown list.

I seem to be running into the problem of the returned XML (or lack there of) If I do a responseText, all is well cept the response is not formatted as XML just a large string.

Here is the Javascript

1<script type="text/javascript">2 /* Create a new XMLHttpRequest object to talk to the Web server */3 var xmlHttp = false;45 function CreateXmlHttp()6 {7 /*@dotnet.itags.org.cc_on @dotnet.itags.org.*/8 /*@dotnet.itags.org.if (@dotnet.itags.org._jscript_version >= 5)910 try11 {12 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");13 } catch (e)14 {15 try16 {17 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");18 }19 catch (e2)20 {21 xmlHttp = false;22 }23 }24 @dotnet.itags.org.end @dotnet.itags.org.*/2526 if (!xmlHttp && typeof XMLHttpRequest != 'undefined')27 {28 xmlHttp = new XMLHttpRequest();29 }30 }313233 function callServ()34 {3536 var site;37 var siteddl = document.getElementById('ddlSites');38 for (var i = 0; i < siteddl.options.length; i++)39 {40 if (siteddl.options[i].selected)41 {42 site = siteddl.options[i].value;43 //document.write(site);44 }45 }46 if (site == '-1')47 {48 return;49 }50 else51 {5253 CreateXmlHttp();5455 var url="popddls.aspx?w=1&id=" + escape(site);565758 xmlHttp.open("GET", url, true);5960 xmlHttp.setRequestHeader("Content-Type", "text/xml");6162 xmlHttp.onreadystatechange = updatePage;6364 xmlHttp.send(null);65 }6667 }6869 function updatePage()70 {71 if(xmlHttp.readyState ==4)72 {73 var response1 = xmlHttp.responseXML;7475 var ddlGrades = document.getElementById('ddlGrades');7677 var grades = response1.getElementsByTagName("grade");78 //ddlGrades.options.length = 0;79 for (var x=0; x < grades.length; x++)80 {81 var id = grades[x].childNodes[0].value;82 var gradedate = grades[x].childNodes[1].value;83 document.write(id + " " + gradedate);84 var newOption = new option();85 newOption.text = gradedate;86 newOption.valueOf = id;87 ddlGrades.options[ddlGrades.length] = newOption;88 }8990 }91 }929394 </script>

Here is the code for popddl which if I run it, it does write the correct XML

1Sub load_gradesddl()2Dim idAs Integer = Request.QueryString("id")3Dim sqlAs String ="select id,gradedate from grades where siteid=" & id4Dim dsAs System.Data.DataSet = DataFunc.FillDS(sql,"gcioq")56 Response.Write("<?xml version=""1.0"" encoding=""utf-8"" ?>")7 Response.Write("<grades>")8For IAs Integer = 0To ds.Tables(0).Rows.Count - 1910 Response.Write("<grade>")11 Response.Write("<id>" & ds.Tables(0).Rows(I).Item("id") &"</id>")12 Response.Write("<gradedate>" & ds.Tables(0).Rows(I).Item("gradedate") &"</gradedate>")13 Response.Write("</grade>")14Next15 Response.Write("</grades>")16End Sub

the error I get in firefox is response1 has no properties. in IE7 I do not get an error but it doesn't work.

What am I doing wrong here?

EDIT:

I also noticed if I do a document.write(grades.length) I get a Zero back though I know the popddl is generating correct XML.

Ok, I got it to work but only in IE7, in firefox it get the right amound of nodes but everything comes out as undefined.

using this to get the data

var id = grades[x].childNodes[0].firstChild.text;

var gradedate = grades[x].childNodes[1].firstChild.text;

Also, this is the XML output

<?xml version="1.0" encoding="utf-8"?>

-<grades>

-<grade>

<id>11</id>

<gradedate>11/27/2007</gradedate>

</grade>

- <grade>

<id>18</id>

<gradedate>11/27/2006</gradedate>

</grade>

-<grade>

<id>19</id>

<gradedate>11/28/2007</gradedate>

</grade>

-<grade>

<id>20</id>

<gradedate>11/29/2007</gradedate>

</grade>

-<grade>

<id>21</id>

<gradedate>11/30/2007</gradedate>

</grade>

</grades>


Ok, I got it to work but only in IE7, in firefox it get the right amound of nodes but everything comes out as undefined.

using this to get the data

var id = grades[x].childNodes[0].firstChild.text;

var gradedate = grades[x].childNodes[1].firstChild.text;

Also, this is the XML output

<?xml version="1.0" encoding="utf-8"?>

-<grades>

-<grade>

<id>11</id>

<gradedate>11/27/2007</gradedate>

</grade>

- <grade>

<id>18</id>

<gradedate>11/27/2006</gradedate>

</grade>

-<grade>

<id>19</id>

<gradedate>11/28/2007</gradedate>

</grade>

-<grade>

<id>20</id>

<gradedate>11/29/2007</gradedate>

</grade>

-<grade>

<id>21</id>

<gradedate>11/30/2007</gradedate>

</grade>

</grades>


Hi,

Thank you or your post!

I suggest you try this for firefox:

var id = grades[x].childNodes[0].firstChild.nodeValue;

var gradedate = grades[x].childNodes[1].firstChild.nodeValue;

If you have further questions,let me know.

Best Regards,