views:

72

answers:

2

I can't believe I'm stuck here, but I can't seem to make a simple HTTP request using Windows script host or simple .hta file.

This is my code:

<script language="Javascript">
window.onload = function() {
    var http = CreateObject("Microsoft.XmlHttp");
};
</script>

When I start the .hta file I get a JavaScript error saying something like Object Expected.. this is very annoying since it's a trivial task and I can't really debug it.

Any idea how I can debug this stuff? Thanks.

+1  A: 

It seems to me you should use

var http = new window.ActiveXObject("Microsoft.XMLHTTP");

or just

var http = new ActiveXObject("Microsoft.XMLHTTP");

inside of .hta file instead of CreateObject().

UPDATED: Hi Luca! I have not much place in comments and can post links not so good, so I appended my answer:

The error which you do from the code of your question is that you try use in JavaScript elements of other language. In VBScript exist CreateObject, but it is a feature of the VBScript language. In JScript/JavaScript you have to use new ActiveXObject instead.

Moreover you mentioned in your comments several times about cross domain problems, but never described what do you do. If you want a help about this subject you should include in your question more information about what do you do. Probably you can include code in WSH or C which worked and include the corresponding version of the .HTA file. It would be also helpful if you describes why you want to use .HTA file instead of WScript/CScript or PowerShell. In which scenario you want to use .HTA file?

Oleg
But I get access denied when trying to request a domain that isn't mine... this seems to be the same behavior as within a browser... is there a way to use native libs to do this? this should be possible.
Luca Matteis
@Luca Matteis: The problem Cross-Domain Security will be solved in other way. For example with Server-side proxy (see with http://www.yaldex.com/ajax-tutorial-4/BBL0044.html). You should describe your scenario more exactly in your question, then one can gives you more exact advices.
Oleg
@Oleg: Sure, I know the browser sandbox can't do cross-site scripting, however .hta isn't entirely a browser sandbox... it gives you full access control through many Windows' apis. What I'm looking for is the API call to make an HTTP request outside the browser sandbox. I hope that makes sense... it's a pretty simple task.
Luca Matteis
@Luca Matteis: In my understanding the usage of .hta file helps you only access all different **local resources** of computer like registry, file system and so on. You can use `new ActiveXObject()` to get any scriptable ActiveX control. But you will not solve Cross-Domain Security problems existing with "Microsoft.XMLHTTP" or `window.XMLHttpRequest`. Of cause you should also follow JavaScript syntax and can't use `CreateObject` which is unknown for JavaScript.
Oleg
It is not unknown to JavaScript... I can perfectly use `WScript.CreateObject()` outside my .hta file.
Luca Matteis
@Oleg: however it seems weird that i'm unable to do cross-domain requests with javascript and I can with VBscript... where's the catch here? This has slowed down my daily hacking... i might just go back to my C coding.
Luca Matteis
@Oleg: I want to use .hta because it provides easy gui (html). I think it's clear enough what I'm trying to do, I'm trying to make an http request and get its result.. for example a simple GET to http://google.com. This works fine with VBscript, but not with JavaScript... how come?
Luca Matteis
@Luca Matteis: It seems that we speak on different languages. `WScript` object with `CreateObject()` method exist **inside of** `WScript.exe` and `CScript.exe`, but there are no `windows` object. On the other side on a HTML page exist `windows` object and `window.XMLHttpRequest` and `window.ActiveXObject` child objects. To create an instance on an object one use `set myVar=CreateObject()` in VB and `var myVar=new MyClass()` in JavaScript. For example if class `window.ActiveXObject` defined, then `var myVar=new window.ActiveXObject("Microsoft.XMLHTTP")` can be used in JavaScript.
Oleg
@Luca Matteis: If you use "Microsoft.XMLHTTP" or native `XMLHttpRequest()` objects you can send HTTP requests. What do you mean if you say "a simple get to google.com"? Do you want ho paint the HTML Page which you see under http://google.com which has also some images and so on? Then you have to make many GET requests. If you want to send HTTP GET to get a data only like XML or JSON data it is another case. Mostly XMLHttpRequest or "Microsoft.XMLHTTP" will be used to get **a part** of HTML page to place it on another page. So how you see I don't understand what you want. Sorry.
Oleg
@Luca Matteis: "Microsoft.XMLHTTP" or native XMLHttpRequest() are designed to get additional piece of HTML , XML or JSON **from the same web site** to make dynamic contain per AJAX. So If your page are loaded from one server you can access only the same server. If you placed .hta file locally you can access only local file system. It you placed it on the local web server you can access only the local web server. See more under http://en.wikipedia.org/wiki/Same_origin_policy.
Oleg
@Luca Matteis: It seems to me that you try to use technique created for one purpose in absolute another situation. If you just want to show a web page you can use WebBrowser Control (see `http://msdn.microsoft.com/en-us/library/aa752040.aspx`). Or you want not display the web page, but to load a data instead?
Oleg
A: 

your function simply creates the object and then destroys it. Try returning to an external variable or doing more in the function.

michael