473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question about JSON and callbacks

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:

Expand|Select|Wrap|Line Numbers
  1. function theClass() {
  2. this.api_key = "2386235627365918365";
  3. this.secret = 889h98ah98h9afaeg8;
  4. this.JSONRequest = function() {
    var params = arguments[0];
  5. for (var i=1; i < arguments.length; i++) {
  6.     params += "&" + arguments[i];
  7. }
  8. params += "&v=1.0";
  9. params += "&sig=" + hex_md5(params.replace(/&/g,"") + this.secret);
  10.  
  11. var requestURL = "http://a.jsonserver.com?" + params;
  12. var headLoc = document.getElementsByTagName("head").item(0);
  13. var scriptObj = document.createElement("script");
  14. scriptObj.setAttribute("type", "text/javascript");
  15. scriptObj.setAttribute("src", requestURL);
  16. headLoc.appendChild(scriptObj);}
  17. this.aFunctionInTheClass = function() {
  18. 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!
May 12 '07 #1
3 1376
pbmods
5,821 Expert 4TB
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 -

Expand|Select|Wrap|Line Numbers
  1. this.authCreateTokenCallback = function() {};
  2.  
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?

Expand|Select|Wrap|Line Numbers
  1. // You might want to consider making params a generic object.
  2. this.JSONRequest = function(params, callback) {
  3. .
  4. .
  5. .
  6. scriptObj.setAttribute("src", requestURL);
  7. headLoc.appendChild(scriptObj);
  8. callback(this, whatever);
  9.  
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:

Expand|Select|Wrap|Line Numbers
  1. this.JSONRequest = function(params, callback) {
  2. .
  3. .
  4. .
  5. var http = new XMLHttpRequest();
  6. // set up your parameters here; it's been so long since I've had to manually create an XMLHttpRequest object...
  7. http.onreadystatechange = function(response) {
  8.     if(http.readyState == 4) {
  9.         eval(response.responseText);
  10.         callback(this, response);
  11.     }
  12. };
  13.  
May 12 '07 #2
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.
May 13 '07 #3
pbmods
5,821 Expert 4TB
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.
May 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

16
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...
20
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... ...
2
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...
3
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);...
5
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). ...
2
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...
23
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...
9
by: Jon Paal [MSMD] | last post by:
using json like ( {"Records": , "RecordCount":"1" } ) and jquery like: $.ajax({ .... success: function(json, status) {
9
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?
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.