Web Connection
AngularJS Callback passing parameters?
Gravatar is a globally recognized avatar based on your email address. AngularJS Callback passing parameters?
  Harvey Mushman
  All
  Jan 26, 2015 @ 08:26am
In my Apps.js file, I have a callback function that is working but I don't think it is working correctly - it works but looks like a kludge...

$http.post('callback.wcsx', $scope.selectedItem).
success(function (data, status, headers, config) {
console.log('item saved' + $scope.selectedItem.pk);
// console.log(status);
// console.log(config);
}).
error(function (data, status, headers, config) {
// log error
console.log(data)
console.log(status)
console.log(config)
});

Now comes the part that seems a bit rough...

lcJSON = Request.cFormVars
lcJSON = IIF(LEFT(lcJson,1)=='&',SUBSTR(lcJSON,2),lcJSON)

Seems like I should be able to pass back the JSON object as a parameter on the QueryString and then do something like lcJSON=Request.Form('json') and have it return the string. Of course this required I add to my callback something that would look like the following:

$http.post('callback.wcsx', 'json=' + $scope.selectedItem).

...but when I look at this on the server, I see where cFormVars = [object Object].

It is working the way it is but this just does not look correct. What am I missing?



--hm--

Gravatar is a globally recognized avatar based on your email address. Re: AngularJS Callback passing parameters?
  Rick Strahl
  Harvey Mushman
  Jan 26, 2015 @ 01:48pm
Hey Jim,

Why are you not using the wwRestProcess class? That makes all of this so much easier:

FUNCTION Callback(loSelectedItem)
...
RETURN loSomeOtherObject
ENDFUNC

You can separate out your API and HTML interfaces into separate extensions/process classes or - as is typically the case with client heavy applications - just add a few HTML methods into your REST Service.

For example in the MusicStore if I wanted to return a dynamic FoxPro generated index.ms page from my REST service class I can simply do:

************************************************************************
* Index
****************************************
*** Function: Return an HTML page for the Index page
************************************************************************

FUNCTION Index()
JsonService.IsRawResponse = .t.
Response.ContentType = "text/html"
Response.ExpandTemplate(Request.GetPhysicalPath())
RETURN
ENDFUNC
* Index

Works just like a regular process class EXCEPT that you have to set the JsonService.IsRawResponse and explicitly set the content type. Likewise you can also return images, pdfs or anything else this way. IOW although a REST service is optimized for JSON it can still act as an HTML service just like a standard wwProcess class from which it inherits - all the features you know from wwProcess are still there.

If you can't use a Rest service class you can also look into a generic Callback handler that uses wwJsonService in a standard wwProcess class:

FUNCTION JsonCallbacks

*** Instantiate or reference the class that will will handle the method call
*** Here I just use the Process class itself which will lhandle the call
* loTarget= CREATEOBJECT("AjaxCallbacks") && Class that handles callback

*** Or point it at your existing wwProcess class so methods are just routed
loTarget = THIS

*** Instanantiate the service
loService = CREATEOBJECT("wwJsonService")

*** Return a JSON Response
Response.ContentType = "application/json"

*** Write out the result from CallMethod which returns the JSON for the method
*** or a JSON Error object if the call failed

lcResult = loService.CallMethod(Request,loTarget) )

*** Return an error response so jQuery's error handler kicks in
*** Note: Result is still a JSON response

IF loService.ErrorOccurred
Response.Status = "500 Server Error"
ENDIF

Response.Write(lcResult)
RETURN

You can then call this method generically with POST data to handle processing of JSON requests:

JsonCallbacks.wcsx?CallbackMethod=HelloWorld

and it will also handle all the JSON conversion for you.

More info here:
wcdocs:_1wu1b8jkq.htm

If you really want to do this manually and need the full request body in your server code:

lcJson = Request.Form()
loSer = CREATE("wwJsonSerializer")
loSelected = loSer.DeserializeJson(lcJson)

Request.Form() will pull out the request data in full without having to parse it.

+++ Rick ---


In my Apps.js file, I have a callback function that is working but I don't think it is working correctly - it works but looks like a kludge...

$http.post('callback.wcsx', $scope.selectedItem).
success(function (data, status, headers, config) {
console.log('item saved' + $scope.selectedItem.pk);
// console.log(status);
// console.log(config);
}).
error(function (data, status, headers, config) {
// log error
console.log(data)
console.log(status)
console.log(config)
});

Now comes the part that seems a bit rough...

lcJSON = Request.cFormVars
lcJSON = IIF(LEFT(lcJson,1)=='&',SUBSTR(lcJSON,2),lcJSON)

Seems like I should be able to pass back the JSON object as a parameter on the QueryString and then do something like lcJSON=Request.Form('json') and have it return the string. Of course this required I add to my callback something that would look like the following:

$http.post('callback.wcsx', 'json=' + $scope.selectedItem).

...but when I look at this on the server, I see where cFormVars = [object Object].

It is working the way it is but this just does not look correct. What am I missing?



Rick Strahl
West Wind Technologies

Making waves on the Web
from Maui

Gravatar is a globally recognized avatar based on your email address. Re: AngularJS Callback passing parameters?
  Harvey Mushman
  Rick Strahl
  Jan 26, 2015 @ 05:42pm
I will have to digest this.... forgive me, I getting really slow in my old age and stuck on what I know works... <g>


Hey Jim,

Why are you not using the wwRestProcess class? That makes all of this so much easier:

FUNCTION Callback(loSelectedItem)
...
RETURN loSomeOtherObject
ENDFUNC

You can separate out your API and HTML interfaces into separate extensions/process classes or - as is typically the case with client heavy applications - just add a few HTML methods into your REST Service.

For example in the MusicStore if I wanted to return a dynamic FoxPro generated index.ms page from my REST service class I can simply do:

************************************************************************
* Index
****************************************
*** Function: Return an HTML page for the Index page
************************************************************************

FUNCTION Index()
JsonService.IsRawResponse = .t.
Response.ContentType = "text/html"
Response.ExpandTemplate(Request.GetPhysicalPath())
RETURN
ENDFUNC
* Index

Works just like a regular process class EXCEPT that you have to set the JsonService.IsRawResponse and explicitly set the content type. Likewise you can also return images, pdfs or anything else this way. IOW although a REST service is optimized for JSON it can still act as an HTML service just like a standard wwProcess class from which it inherits - all the features you know from wwProcess are still there.

If you can't use a Rest service class you can also look into a generic Callback handler that uses wwJsonService in a standard wwProcess class:

FUNCTION JsonCallbacks

*** Instantiate or reference the class that will will handle the method call
*** Here I just use the Process class itself which will lhandle the call
* loTarget= CREATEOBJECT("AjaxCallbacks") && Class that handles callback

*** Or point it at your existing wwProcess class so methods are just routed
loTarget = THIS

*** Instanantiate the service
loService = CREATEOBJECT("wwJsonService")

*** Return a JSON Response
Response.ContentType = "application/json"

*** Write out the result from CallMethod which returns the JSON for the method
*** or a JSON Error object if the call failed

lcResult = loService.CallMethod(Request,loTarget) )

*** Return an error response so jQuery's error handler kicks in
*** Note: Result is still a JSON response

IF loService.ErrorOccurred
Response.Status = "500 Server Error"
ENDIF

Response.Write(lcResult)
RETURN

You can then call this method generically with POST data to handle processing of JSON requests:

JsonCallbacks.wcsx?CallbackMethod=HelloWorld

and it will also handle all the JSON conversion for you.

More info here:
wcdocs:_1wu1b8jkq.htm

If you really want to do this manually and need the full request body in your server code:

lcJson = Request.Form()
loSer = CREATE("wwJsonSerializer")
loSelected = loSer.DeserializeJson(lcJson)

Request.Form() will pull out the request data in full without having to parse it.

+++ Rick ---


In my Apps.js file, I have a callback function that is working but I don't think it is working correctly - it works but looks like a kludge...

$http.post('callback.wcsx', $scope.selectedItem).
success(function (data, status, headers, config) {
console.log('item saved' + $scope.selectedItem.pk);
// console.log(status);
// console.log(config);
}).
error(function (data, status, headers, config) {
// log error
console.log(data)
console.log(status)
console.log(config)
});

Now comes the part that seems a bit rough...

lcJSON = Request.cFormVars
lcJSON = IIF(LEFT(lcJson,1)=='&',SUBSTR(lcJSON,2),lcJSON)

Seems like I should be able to pass back the JSON object as a parameter on the QueryString and then do something like lcJSON=Request.Form('json') and have it return the string. Of course this required I add to my callback something that would look like the following:

$http.post('callback.wcsx', 'json=' + $scope.selectedItem).

...but when I look at this on the server, I see where cFormVars = [object Object].

It is working the way it is but this just does not look correct. What am I missing?




--hm--

© 1996-2024