I'm trying to figure out how to get the Json response in ASP/JavaScript. I basically need the country code of an IP address to verify whether we have shipping rights there or not. I have all the other code, but I can't for the life of me figure out what in the world I'm supposed to do to implement this service into the browser.
Basically from my understanding, I need to set the Header to Accept the Mime type application/json, from the arin link I query.
This is the code I have, although I haven't tested it, I am so in the dark, I don't think I'd be getting what I need, which is the JSON object.
I have googled galore for help, with no avail, so any help would be awesome; I'm completely clueless as to how to even go about this. The code below is pretty much my "shot in the dark" at it.
Code:
function getUserCountry(){
var strResult;
strURL = "http://whois.arin.net/rest/ip/"+Request.ServerVariables("remote_addr");
try
{
// Create the WinHTTPRequest ActiveX Object.
var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
// Create an HTTP request.
var temp = WinHttpReq.Open("GET", strURL, false);
// Send the HTTP request.
WinHttpReq.Send();
// Retrieve the response text.
strResult = WinHttpReq.ResponseText;
}
catch (objError)
{
strResult = objError + "\n"
strResult += "WinHTTP returned error: " +
(objError.number & 0xFFFF).toString() + "\n\n";
strResult += objError.description;
}
// Return the response text.
return strResult;
}
Last edited by MissPhoenix; 04-01-2011 at 07:35 PM..
Reason: (updating title)
??? If you are using new ActiveXObject then this code must be for use in the browser, not in your ASP page. And if that is so, then it's also so that using new ActiveXObject means your code will only work in MSIE browsers. Do you care?
But in any case, if this is an in-the-browser question, it's not an ASP question, per se.
If you really intended this to be an ASP question, using JScript in ASP, then you should not use new ActiveXObject, you should use Server.CreateObject( ). But then I'd question the use of JSON. It would probably be easier to request an XML response and parse that. Which, yes, I do in ASP code (albeit with VBScript, not JScript).
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
But I still have to ask if you are doing this in ASP code or in browser code. Because if you are doing it in browser code, you are using an obsolescent object. MSIE 8 and above use the standard object that all the other browsers use and don't need to use ActiveXObject().
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
ASP Classic with JavaScript. And I fixed the code I presented, in case someone else needs to know in the future. Thank you, very much Pedant. You're always the one who answers my convoluted ASP questions... LOL
I heard many many years ago (about 10 years, in fact) that for server side code, you got better efficiency using Server.CreateObject than using ActiveXObject. It's quite possible that the advice is out of date, what with much newer version of the ASP engine in current IIS servers. But I would have code it thus:
Code:
var xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP");
The reason, as I recall it, was that objects created in the context of the built-in SERVER container could be destroyed en masse and didn't need individual garbage collection.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.