Jump to content

JSONP vs JSON


skaterdav85

Recommended Posts

So I recently heard about JSONP and was hoping someone could clarify my understanding. From my understanding, JSONP is optimal for making requests through the client-side (using JS) because you cant make JSON requests and retrieve data using javascript unless the script retrieving the JSON data is on the same domain as the JSON data. To make requests for JSON data, you would either need to do this on the server or the JSON data would need to be within the same domain as the script that is retrieving it. Is this correct?

Link to comment
Share on other sites

JSONP is a technique of passing data between servers using JSON. It's not an alternative to JSON.Basically it just involves loading JavaScript from another site, but all that JS contains is some JSON, with padding (hence the P). E.g.

// http://domain2.com/json.jstell({"some" : "json"});

<!-- http://domain1.com/page.html --><script type="text/javascript">	received_data;	var tell = function(json) {		received_data = json;	}</script><script type="text/javascript" src="http://domain2.com/json.js"></script>

The padding is necessary, because, well, if the JS document did just contain JSON, how would you store it?

Link to comment
Share on other sites

what do you mean by padding? and in your example, what is 'received_data'? a variable?Also, I was looking at a script today and it looks like they were creating some sort of JS object. Is the following JSON?

var search = {				selector : 'body',				term : "and",				getTerms : function(){}};

Link to comment
Share on other sites

The "padding" is the function that stores the incoming data. received_data is a global variable.

Also, I was looking at a script today and it looks like they were creating some sort of JS object. Is the following JSON?
Yes. Remember it does stand for JavaScript Object Notation - it's original purpose was to provide a concise way of describing objects.
Link to comment
Share on other sites

Not really... Object Oriented JavaScript is the general use of the JavaScript language in an object-oriented fashion - that is, to solve problems using a data-centric methodology, with objects. JSON is just a syntactic detail.If a question asked, "write a script to do <blah>, using object-oriented techniques", just using JSON would not be sufficient. :)

Link to comment
Share on other sites

Good point. I found a pretty good tutorial on object oriented javascript which cleared up a lot of questions I had about it. The tutorial is here, and it looks like json is one way of creating objects in JS, but using the constructor object seems more flexible since properties can be dynamic.I noticed the term 'namespace' being used on some other sites regarding oop in js. Do you know what that means?

Link to comment
Share on other sites

A namespace is similar to a scope. Javascript doesn't explicitly support namespaces, so namespaces in Javascript are basically objects or scopes used in a way that approximate actual namespaces in other languages. For example, Javascript does not have a function called alert, the method is actually window.alert. But when you execute code in Javascript, the default scope or namespace is the window object. So you can use the properties and methods of the window object without needing to specify it. You can also define your own object with an alert method, and even though both window and your object have a method called alert, since they're defined in different scopes you can use both of them without a name conflict, e.g.:

var obj = {  alert: function()  {	window.alert('this is from the custom alert');  }};obj.alert();

http://en.wikipedia.org/wiki/Namespace_(computer_science)

Link to comment
Share on other sites

You'll find that libraries (like jQuery) use "namespaces" a lot. Having a lot of global/public variables and functions can be dangerous when you're essentially blending two distinct projects into one. If you had a function called foo() and your library had a function called foo(), one of them would get clobbered, and your app would break.One solution is to create a "namespace" object. Here's an example (not so different from jsg's, but I'm discussing it a little differently):

bigdave = {};bigdave.foo = function () {   alert("bar");};bigdave.date = new Date();// and so on

This significantly reduces the chances of overwriting (what are the chances of importing a library that also has an object called "bigdave"?). And if your app really really really needs a bunch of global/static variables, this is a more developer-friendly way to keep track of them.

Link to comment
Share on other sites

It's funny you mention jQuery because that was the primary reason I was asking about namespaces. I was confused between jQuery.somemethod() versus jQuery('some selector').somemethod(). Apparently there is one namespace ($.fn) aka the “jQuery prototype,” which acts on jQuery selected objects. And then there are methods that dont act on a selection (such as utility methods) and these are part of the jQuery namespace and are accessed like jQuery.someMethod(). Conceptually it makes sense. I'm just kind of curious as to what these objects looks like in regular JS.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...