Ajax

From NSB App Studio
Revision as of 16:43, 13 November 2013 by Ghenne (talk | contribs) (→‎Description)
Jump to navigation Jump to search

Ajax(URL[, method[, data[, returnFunction]]])

Description

The Ajax function is used to get or receive data from a server. It is based on XMLHttpRequest. This function will only work when deployed to the website. It will not work when running in the local browser.

URL is the address on the server. It can be the name of a file to be downloaded from the server or a script to be run on the server. Optional information can be passed to the server at the end of the URL: for example

 http://www.nsbasic.com/getCustomerInfo.php?customer=Knuth 

will call the PHP script named getCustomerInfo.php, passing the parameter customer=Knuth.

Use method to define the access method. If this parameter is omitted, GET is used. The most common value for this is POST. POST responses are never cached, whereas GET responses can be. POST also allows larger data transfers. If you are using POST, put the information you want to send in data. The normal size limit for data is 8 megabytes: it is a server setting which can be increased. This is an easy and efficient way to send data to a server.

returnFunction, if supplied, is a function to be called when the results return, allowing asynchronous execution. If this parameter is not supplied, execution will continue at the next statement after the results return. Do not put MsgBox statements inside your Ajax code: they mess up the event sequence.

Other useful methods are DELETE and PUT. For more information on these options, see XMLHttpRequest's open method. For more information on other methods see HTTP request methods.

It returns the following values:

.status: The status of the response to the request. This is the HTTP result code (for example, status is 200 for a successful request). 404 means page not found.

.statusText: The response string returned by the HTTP server. Unlike status, this includes the entire text of the response message ("200 OK", for example).

.responseText: The response to the request as text, or null if the request was unsuccessful or has not yet been sent.

.readyState: The state of the request:

State  Description
0      UNSENT:The request is not initialized
1      OPENED: The request has been set up
2      HEADERS_RECEIVED send() has been called, and headers and status are available.
3      LOADING: Downloading; responseText holds partial data.
4      DONE: The request is complete

See also samples Ajax and AjaxPost for more information.

Example (BASIC)

'Ajax example
req = Ajax("/sendData_ajax.php/?myText=" + txtSend.value) 
If req.status = 200 Then 'success
    txtResponse.value = req.responseText
Else 'failure
    txtResponse.value = "Error: " & req.err.message
End If

'Sample POST call
req = Ajax("/sendData_ajaxPost.php", "POST", "myText=" & txtSend.value)

Example (JavaScript)

//Ajax example
req = Ajax("/sendData_ajax.php/?myText=" + txtSend.value);
if(req.status == 200) { //success
    txtResponse.value = req.responseText;
  } else { //failure
    txtResponse.value = "Error: "  +  req.err.message;
}

 //Sample POST call
req = Ajax("/sendData_ajaxPost.php" , "POST" , "myText="  +  txtSend.value);

Related Items