GetJSON: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
(Created page with "getJSONP(''url'',''data'',''callback'') == Description == Note: This function can be replaced by the Ajax() function. ReadFile will read ''filename'' which can either b...")
 
No edit summary
Line 3: Line 3:
== Description ==
== Description ==


Note: This function can be replaced by the [[Ajax]]() function.
getJSONP loads JSON-encoded data from the server using a GET HTTP request.  


ReadFile will read ''filename'' which can either be deployed with the app or be on the same server as the app. If the file is to be deployed with the app, include it in the manifest. This function will only work when deployed to a website: it will not work when running in the local browser.  
''url'' is the location that data is being requested from.


Use ''method'' to define the access method. Usually this parameter can be left out: the default is Get. The next most common value for this is Post. Post responses are never cached, whereas Get responses can be. Post also allows larger data transfers. Other methods are Connect, Delete, Head, Options, Put, Trace, or Track. For more information on these options, look up XMLHttpRequest on the web. The function is based on XMLHttpRequest. See the ReadFile.nsx sample.
''data'' is the information about the request. If multiple fields are being passed, include them in a single string, separated by ampersand (&) characters. It's best to do an http://wiki.nsbasic.com/EncodeURIComponent/decodeURIComponent() to the data to make sure that spaces and other special characters are properly formatted.


It returns two values: .status = 0 or 200 if the file was read successfully and .responseText which has the entire contents of the file. If the file is not read successfully, a different status code is returned in .status.
''callback'' is the name of the function in your program to be called when the request is complete. Since the request may take a little while to complete, your app will continue execution immediately after the getJSONP() function is called. The results will not be available until the ''callback'' function is called. The returned data is passed to the ''callback'' function.
 
There is no equivilent WriteFile. Use [[sql|Sql]], [[localstorage|LocalStorage]] or the PhoneGap API to save data.


== Example ==
== Example ==


<pre>
<pre>
Rem ReadfileFile example
Function btnLoad_onclick()
filename="g.txt"
  Dim city = encodeURIComponent(txtCity.value)
req=ReadFile(filename)
  GetJSONP("http://api.openweathermap.org/data/2.5/weather","q=" & city, weatherData)
If req.status=200 Then
End Function
  MsgBox req.responseText
 
Else
Sub weatherData(data)
   MsgBox "File could not be read"
   MsgBox "Temperature is " & data.main.temp - 273.15
End If
End Sub
</pre>
</pre>



Revision as of 15:32, 15 January 2014

getJSONP(url,data,callback)

Description

getJSONP loads JSON-encoded data from the server using a GET HTTP request.

url is the location that data is being requested from.

data is the information about the request. If multiple fields are being passed, include them in a single string, separated by ampersand (&) characters. It's best to do an http://wiki.nsbasic.com/EncodeURIComponent/decodeURIComponent() to the data to make sure that spaces and other special characters are properly formatted.

callback is the name of the function in your program to be called when the request is complete. Since the request may take a little while to complete, your app will continue execution immediately after the getJSONP() function is called. The results will not be available until the callback function is called. The returned data is passed to the callback function.

Example

Function btnLoad_onclick()
  Dim city = encodeURIComponent(txtCity.value)
  GetJSONP("http://api.openweathermap.org/data/2.5/weather","q=" & city, weatherData)
End Function

Sub weatherData(data)
  MsgBox "Temperature is " & data.main.temp - 273.15
End Sub

Related Items

Ajax