Hi guys, I was hoping you could help me wrap my head around something. I just started with JSON today and am loving it, but I'm having a bit of a conceptual problem. Here's what's going on:
I have a class, let's call it theClass, like this: - function theClass() {
-
this.api_key = "2386235627365918365";
-
this.secret = 889h98ah98h9afaeg8;
-
this.JSONRequest = function() {
var params = arguments[0]; -
for (var i=1; i < arguments.length; i++) {
-
params += "&" + arguments[i];
-
}
-
params += "&v=1.0";
-
params += "&sig=" + hex_md5(params.replace(/&/g,"") + this.secret);
-
-
var requestURL = "http://a.jsonserver.com?" + params;
-
var headLoc = document.getElementsByTagName("head").item(0);
-
var scriptObj = document.createElement("script");
-
scriptObj.setAttribute("type", "text/javascript");
-
scriptObj.setAttribute("src", requestURL);
-
headLoc.appendChild(scriptObj);}
-
this.aFunctionInTheClass = function() {
-
var request = this.JSONRequest("api_key=" + this.api_key, "callback=authCreateTokenCallback", "method=some_method");
-
There's obviously a lot more to this class, but I don't want to bore you. So, someone should be able to say: var classInstance = new theClass(); and be able to call various functions in the class, one of which is this JSONRequest function. OK, well the problem lies in the callback. I need the callback to point to another function in theClass which sets some other member variables of theClass and such. Right now, the only way I can get this to work is to put the callback function outside the class, which is useless to me. If I try to put something like this.authCreateTokenCallback = function {} in theClass, I just get an error saying "Object expected" or something like that. How can I get the callback to point to that member function?
I hope I wasn't too confusing and thanks for your help!
3 1376
If I try to put something like this.authCreateTokenCallback = function {} in theClass, I just get an error saying "Object expected" or something like that.
You need parenthesis for that - -
this.authCreateTokenCallback = function() {};
-
How can I get the callback to point to that member function?
If I understand your code correctly, you are loading a new script object whose source is dynamically generated by requestURL. You're passing callback as a parameter to the requestURL, but the callback isn't actually used by server; instead, you just want to invoke the callback when the script loads. Is that correct? - // You might want to consider making params a generic object.
-
this.JSONRequest = function(params, callback) {
-
.
-
.
-
.
-
scriptObj.setAttribute("src", requestURL);
-
headLoc.appendChild(scriptObj);
-
callback(this, whatever);
-
It's difficult to make this work effectively, since some browsers (FIrefox is a notable example) will load dynamic script elements asynchronously. In other words, when you attach the new script, Firefox won't wait until it has finished parsing the new file before executing the next line in your function.
Your best bet would be to create a new XMLHttpRequest object and eval the response text. Then you can set the object's onreadystatechange handler to an anonymous function that invokes the callback if readyState == 4.
A bare bones example: -
this.JSONRequest = function(params, callback) {
-
.
-
.
-
.
-
var http = new XMLHttpRequest();
-
// set up your parameters here; it's been so long since I've had to manually create an XMLHttpRequest object...
-
http.onreadystatechange = function(response) {
-
if(http.readyState == 4) {
-
eval(response.responseText);
-
callback(this, response);
-
}
-
};
-
Unfortunately, the entire reason I'm using JSON to do this is because I don't want to use XMLHttpRequests because I am doing cross-domain calls. It works OK in IE if the option to allow cross-domain calls is turned on, but it won't work in Firefox at all. I'll need to get my script signed and that seems like a big hassle.
I also thought about the asynchronous problem and it's actually a big problem. Not sure how I'll solve that.
Alternatively, do you know an easy way to get a script signed in Windows? I explored the options on mozilla.org and the tool they provide is for *nix only.
Unfortunately, the entire reason I'm using JSON to do this is because I don't want to use XMLHttpRequests because I am doing cross-domain calls. It works OK in IE if the option to allow cross-domain calls is turned on, but it won't work in Firefox at all. I'll need to get my script signed and that seems like a big hassle.
This is done to prevent cross-site scripting attacks. For example, you could write a script that would send 100,000 XMLHttpRequests to your least favorite person's homepage, effectively initiating a DoS attack. Get enough people to run your script, and....!
Alternatively, do you know an easy way to get a script signed in Windows? I explored the options on mozilla.org and the tool they provide is for *nix only.
Not a clue. One way to work around this, though is to write a script on the server side that loads the information from the other sites, and then call this script via XMLHttpRequest.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: G Matthew J |
last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm
This is more or less in response to Mr Crockford's admonition a few
months ago, "fork if you must". Ironically, although in that usenet
post...
|
by: Luke Matuszewski |
last post by:
Welcome
As suggested i looked into JSON project and was amazed but...
What about cyclical data structures - anybody was faced it in some
project ?
Is there any satisactional recomendation...
...
|
by: Kevin Newman |
last post by:
Hello,
I noticed that the JavaScript library for JSON posted on json.org
(http://www.json.org/json.js) is modifying Object.prototype (adding a
method - toJSONString). I thought this was...
|
by: Adam |
last post by:
I'm trying to retrieve some values from a json object, but instead it's
giving me the property name. For example:
var json = { "glossary": { "title": "example glossary" } };
console.log(json);...
|
by: Tom Cole |
last post by:
Let's say I have the following JSON string returned from a server-side
process:
{ values: }
I then create a JSON object from it using eval() (tell me if this is
not what should be done).
...
|
by: ChrisO |
last post by:
I've been pretty infatuated with JSON for some time now since
"discovering" it a while back. (It's been there all along in
JavaScript, but it was just never "noticed" or used by most until...
|
by: dhtmlkitchen |
last post by:
JSON
We all know what it is.
In ECMAScript 4, there's a JSON proposal:
Object.prototype.toJSONString
String.prototype.parseJSON
The current proposal, String.prototype.parseJSON, returns...
|
by: Jon Paal [MSMD] |
last post by:
using json like
( {"Records": , "RecordCount":"1" } )
and jquery like:
$.ajax({
....
success: function(json, status) {
|
by: BryanA |
last post by:
Is it possible to parse JSON data and then post it to a mysql db with
the data in their respective fields?
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |