Okay, obviously, I am not grokking this whole JavaScript interfacing thing. I've got to find a way to wrap my head around it.
-- According to this webpage: http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax I can use jQuery's beforeSend callback to add a HTTP header with authentication information:
So in my Amber code, I tried this:
beforeSend: [:xhr | |enc| enc = window btoa: 'usr:pwd'. xhr setRequestHeader: 'Authorization' put: 'Basic ',enc]
It does not understand btoa:, even though the following works perfectly in the Workspace:
When I get rid of the btoa stuff, it complains that it does not understand setRequestHeader:. I've looked at this: https://github.com/amber-smalltalk/amber/wiki/From-smalltalk-to-javascript-and-back And it confuses me. What am I doing wrong??? Btw, I'm not having much luck with '< >', either. You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
Hi Richard,
not knowing if your code would work, I would still think you have to change your assignments from "=" to ":="
beforeSend: [:xhr | |enc| enc := window btoa: 'usr:pwd'. xhr setRequestHeader: 'Authorization' put: 'Basic ',enc]
Sebastian On 2015-05-29 4:21 PM, Richard Eng
wrote:
-- You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
Say, you're right! Stupid typo.
-- Okay, but now, it doesn't recognize setRequestHeader:put:. (If I get rid of put:, it doesn't recognize setRequestHeader:, so apparently it's not mapping setRequestHeader from JS. On Friday, 29 May 2015 20:18:30 UTC-4, HCSebastian wrote:
You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
In reply to this post by horrido
Hmmm.....
I jjust read that getJson is a shorthand and one can't set additional options... Did you already consider to do it this way? ---------------------------------- get: aUrlString do: aBlock onError: errorBlock jQuery ajax: aUrlString options: #{ 'type' -> 'GET'. 'contentType' -> 'application/json'. 'beforeSend' -> [:xhr | |enc| enc = window btoa: 'usr:pwd'. xhr setRequestHeader: 'Authorization' with: 'Basic ',enc].'complete' -> [:res | res status = 200 ifTrue: [aBlock value: res responseText] ifFalse: [errorBlock value: res responseText] } --------------------------------------- or something like: jQuery = JQuery current. jQuery ajaxSetup: #{ 'headers' -> #{ 'Authorization' : 'Basic
faskd52352rwfsdfs' ; }}. jQuery getJSON: 'some-url'
Just guessing the seperators, I still struggle with the right
Array {} syntax. On 2015-05-29 4:21 PM, Richard Eng
wrote:
-- You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
As for `window btoa: 'foo'`, it is preferable not to use `window` to access globals, but instead use them directly as in `btoa value: 'foo'`.
As already mentioned, there was wrong assignment in your code (I would not use assignment at all and write `JQuery current foo: 'bar'`, but it's just my preference). Otherwise, there should be no problems with using JS calls - though for simplicity I always used single-arg ajax: #{ 'url' -> 'baz'. ... } send (and instead of setting callbacks in options, which is IIRC deprecated, I used done: and friends on the result of ajax: send). Herby Dňa 30. mája 2015 4:02:36 CEST používateľ 'Sebastian Heidbrink' via amber-lang <[hidden email]> napísal:
-- Hmmm..... You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
Okay, I'm trying to follow your suggestions. This works: JQuery current getJSON: 'https://miramar21.com/tut_server/default/api/verify/person/[hidden email]/Prometheus' onSuccess: [:jsonData | alert value: 'verified is ',(jsonData at: 'verified')]. JQuery current ajax: #{'type' -> 'GET'. 'url' -> 'https://miramar21.com/tut_server/default/api/verify/person/[hidden email]/Prometheus'. 'dataType' -> 'json'} onSuccess: [:jsonData | alert value: 'verified is ',(jsonData at: 'verified')]. onSuccess: is never executed. I don't understand to what class onSuccess: belongs; it doesn't appear to be part of jQuery. I've never heard of done:. On Saturday, 30 May 2015 00:56:43 UTC-4, Herby wrote:
You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
Richard Eng wrote: > Okay, I'm trying to follow your suggestions. This works: > > JQuery current getJSON: > 'https://miramar21.com/tut_server/default/api/verify/person/horrido.hobbies@.../Prometheus' > onSuccess: [:jsonData | alert value: 'verified is ',(jsonData at: > 'verified')]. > > But when I try to convert to using the ajax call: > > JQuery current ajax: #{'type' -> 'GET'. > 'url' -> > 'https://miramar21.com/tut_server/default/api/verify/person/horrido.hobbies@.../Prometheus'. > 'dataType' -> 'json'} > onSuccess: [:jsonData | alert value: 'verified is ',(jsonData at: > 'verified')]. > > onSuccess: is never executed. I don't understand to what class > onSuccess: belongs; it doesn't appear to be part of jQuery. ??? I don't get this sentence and question in it at all. > > I've never heard of done:. done, fail, always. Look up jQuery.ajax docs page. > > > On Saturday, 30 May 2015 00:56:43 UTC-4, Herby wrote: > > As for `window btoa: 'foo'`, it is preferable not to use `window` > to access globals, but instead use them directly as in `btoa > value: 'foo'`. > > As already mentioned, there was wrong assignment in your code (I > would not use assignment at all and write `JQuery current foo: > 'bar'`, but it's just my preference). > > Otherwise, there should be no problems with using JS calls - > though for simplicity I always used single-arg ajax: #{ 'url' -> > 'baz'. ... } send (and instead of setting callbacks in options, > which is IIRC deprecated, I used done: and friends on the result > of ajax: send). > > Herby > > Dňa 30. mája 2015 4:02:36 CEST používateľ 'Sebastian Heidbrink' > via amber-lang <[hidden email] <javascript:>> napísal: > > Hmmm..... > > I jjust read that getJson is a shorthand and one can't set > additional options... > > Did you already consider to do it this way? > --------------------------- > get: aUrlString do: aBlock onError: errorBlock > > jQuery ajax: aUrlString > options: #{ > 'type' -> 'GET'. > 'contentType' -> 'application/json'. > 'beforeSend' -> [:xhr | |enc| enc = window btoa: 'usr:pwd'. > > xhr setRequestHeader: 'Authorization' with: 'Basic ',enc]. > > 'complete' -> [:res | > res status = 200 > ifTrue: [aBlock value: res responseText] > ifFalse: [errorBlock value: res responseText] > } > --------------------------------------- > > or something like: > > jQuery = JQuery current. > > jQuery ajaxSetup: #{ > 'headers' -> #{ > |'Authorization':'Basic faskd52352rwfsdfs'|; > ||} > }. > > jQuery getJSON: 'some-url' > > onSuccess: [:jsonData | "do something with jsonData"]. > > > > |Just guessing the seperators, I still struggle with the right > Array {} s > > Sebastian > | > On 2015-05-29 4:21 PM, Richard Eng wrote: >> Okay, obviously, I am not grokking this whole JavaScript >> interfacing thing. I've got to find a way to wrap my head >> around it. >> >> According to this webpage: >> http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax >> <http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax> >> >> I can use jQuery's /beforeSend/ callback to add a HTTP header >> with authentication information: >> >> beforeSend: function (xhr) { >> xhr.setRequestHeader ("Authorization", "Basic XXXXXX"); >> }, >> >> >> So in my Amber code, I tried this: >> >> jQuery = JQuery current. >> jQuery getJSON: 'some-url' >> >> beforeSend: [:xhr | |enc| enc = window btoa: 'usr:pwd'. >> >> xhr setRequestHead >> >> onSuccess: [:jsonData | "do something with jsonData"]. >> >> >> It does not understand btoa:, even though the following works >> perfectly in the Workspace: >> >> window btoa: 'usr:pwd' >> >> >> When I get rid of the btoa stuff, it complains that it does >> not understand setRequestHeader:. >> >> I've looked at this: >> https://github.com/amber-smalltalk/amber/wiki/From-smalltalk-to-javascript-and-back >> <https://github.com/amber-smalltalk/amber/wiki/From-smalltalk-to-javascript-and-back> >> >> And it confuses me. What am I doing wrong??? >> >> Btw, I'm not having much luck with '< >', either. >> -- >> You received this message because you are subscribed to the >> Google Groups "amber-lang" group. >> To unsubscribe from this group and stop receiving emails from >> it, send an email to amber-lang+ >> <javascript:>. >> For more options, visit https://groups.google.com/d/optout >> <https://groups.google.com/d/optout>. > > -- > You received this message because you are subscribed to the Google > Groups "amber-lang" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to [hidden email] > <mailto:[hidden email]>. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
In reply to this post by horrido
Richard Eng wrote: > I've looked at this: > https://github.com/amber-smalltalk/amber/wiki/From-smalltalk-to-javascript-and-back > > And it confuses me. What am I doing wrong??? Well, the fact is, you just MUST understand this (you can try to reword it, if you think it can be done better, it's a wiki). Until you don't get it, just reread it over and over until you do. I had the impression the examples that are there cover all possibilities for understanding it by example, if it's not the case, help with fixing it. This is in fact very important part, as Amber is aimed at "take JS library for the task and use it" solutions. -- You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
In reply to this post by Herby Vojčík
Apparently, I'm not coding the ajax call correctly, because onSuccess: is not executed, i.e., I don't get an alert popup. When I used getJSON:, I did get an alert popup. So why isn't ajax: working?
-- As for onSuccess:, my question is whether it's associated with jQuery. jQuery has a 'success' callback, but it doesn't seem to take a parameter (i.e., the json result). onSuccess: does have a json result parameter. Ultimately, my question is, how do I obtain the json result from the ajax call? On Saturday, 30 May 2015 08:50:59 UTC-4, Herby wrote:
You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
Richard Eng wrote: > Apparently, I'm not coding the ajax call correctly, because onSuccess: > is not executed, i.e., I don't get an alert popup. When I used > getJSON:, I did get an alert popup. So why isn't ajax: working? > > As for onSuccess:, my question is whether it's associated with jQuery. > jQuery has a 'success' callback, but it doesn't seem to take a > parameter (i.e., the json result). onSuccess: does have a json result > parameter. Ultimately, my question is, how do I obtain the json result > from the ajax call? Yes, from this it is clear that you do not understand how to rewrite JS call to ST message send and you just copied getJSON:onSuccess: from the example without understanding it. You really must understand how it is done, or these questions never end. Try to reread that wiki page until you get it (onSuccess: is totally unimportant. You can write froznicate:, foobar: or whatever else there; see how keyword message for multiple-arg calls are created and how other keywords beyond the first are not used at all beyond the fact that it separates second parameter from the first). > > On Saturday, 30 May 2015 08:50:59 UTC-4, Herby wrote: > > > > Richard Eng wrote: > > Okay, I'm trying to follow your suggestions. This works: > > > > JQuery current getJSON: > > > 'https://miramar21.com/tut_server/default/api/verify/person/horrido.hobbies@.../Prometheus > <https://miramar21.com/tut_server/default/api/verify/person/horrido.hobbies@.../Prometheus>' > > > onSuccess: [:jsonData | alert value: 'verified is ',(jsonData at: > > 'verified')]. > > > > But when I try to convert to using the ajax call: > > > > JQuery current ajax: #{'type' -> 'GET'. > > 'url' -> > > > 'https://miramar21.com/tut_server/default/api/verify/person/horrido.hobbies@.../Prometheus > <https://miramar21.com/tut_server/default/api/verify/person/horrido.hobbies@.../Prometheus >'. > > > 'dataType' -> 'json'} > > onSuccess: [:jsonData | alert value: 'verified is ',(jsonData at: > > 'verified')]. > > > > onSuccess: is never executed. I don't understand to what class > > onSuccess: belongs; it doesn't appear to be part of jQuery. > > ??? > I don't get this sentence and question in it at all. > > > > > I've never heard of done:. > > done, fail, always. Look up jQuery.ajax docs page. > > > > > > > On Saturday, 30 May 2015 00:56:43 UTC-4, Herby wrote: > > > > As for `window btoa: 'foo'`, it is > preferable not to use `window` > > to access globals, but instead use them directly as in `btoa > > value: 'foo'`. > > > > As already mentioned, there was wrong assignment in your code (I > > would not use assignment at all and write `JQuery current foo: > > 'bar'`, but it's just my preference). > > > > Otherwise, there should be no problems with using JS calls - > > > 'baz'. ... } send (and instead of setting callbacks in options, > > which is IIRC deprecated, I used done: and friends on the result > > of ajax: send). > > > > Herby > > > > Dňa 30. mája 2015 4:02:36 CEST používateľ 'Sebastian Heidbrink' > > via amber-lang <[hidden email] <javascript:>> napísal: > > > > Hmmm..... > > > > I jjust read that getJson is a shorthand and one can't set > > additional options... > > > > Did you already consider to do it this way? > > --------------------------- > ------- > > get: aUrlString do: aBlock onError: errorBlock > > > > jQuery ajax: aUrlString > > options: #{ > > 'type' -> 'GET'. > > 'contentType' -> 'application/json'. > > 'beforeSend' -> [:xhr | |enc| enc = window btoa: 'usr:pwd'. > > > > xhr setRequestHeader: 'Authorization' with: 'Basic ',enc] > > > > 'complete' -> [:res | > > res status = 200 > > ifTrue: [aBlock value: res responseText] > > ifFalse: [errorBlock value: res responseText] > > } > > --------------------------------------- > > > > or something like: > > > > jQuery = JQuery current. > > > > jQuery ajaxSetup: #{ > > 'headers' -> #{ > > |'Authorization':'Basic faskd52352rwfsdfs'|; > > ||} > > }. > > > > jQuery getJSON: 'some-url' > > > > onSuccess: [:jsonData | "do something with jsonData"]. > > > > > > > > |Just guessing the seperators, I still struggle with the right > > Array {} s > yntax. > > > > Sebastian > > | > > On 2015-05-29 4:21 PM, Richard Eng wrote: > >> Okay, obviously, I am not grokking this whole JavaScript > >> interfacing thing. I've got to find a way to wrap my head > >> around it. > >> > >> According to this webpage: > >> > http://stac > <http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax> > > >> > <http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax > <http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax>> > > >> > >> I can use jQuery's /beforeSend/ callback to add a HTTP header > >> with authentication information: > >> > >> beforeSend: function (xhr) { > >> xhr.setRequestHeader ("Authorization", "Basic XXXXXX"); > >> }, > >> > >> > >> So in my Amber code, I tried this: > >> > >> jQuery = JQuery current. > >> jQuery getJSON: 'some-url' > >> > >> beforeSend: [:xhr | |enc| enc = window btoa: 'usr:pwd'. > >> > >> xhr setRequestHead > er: 'Authorization' put: 'Basic ',enc] > >> > >> onSuccess: [:jsonData | "do something with jsonData"]. > >> > >> > >> It does not understand btoa:, even though the following works > >> perfectly in the Workspace: > >> > >> window btoa: 'usr:pwd' > >> > >> > >> When I get rid of the btoa stuff, it complains that it does > >> not understand setRequestHeader:. > >> > >> I've looked at this: > >> > https://github.com/amber-smalltalk/amber/wiki/From-smalltalk-to-javascript-and-back > <https://github.com/amber-smalltalk/amber/wiki/From-smalltalk-to-javascript-and-back> > > >> > <https://github.com/amber-smalltalk/amber/wiki/From-smalltalk-to-javascript-and-back > <https://github.com/amber-smalltalk/amber/wiki/From-smalltalk-to-javascript-and-back>> > > >> > >> And it confuses me. What am I doing wrong??? > >> > >> Btw, I'm not having much luck with '< >', either. > >> -- > >> You received this message because you are subscribed to the > >> Google Groups "amber-lang" group. > >> To unsubsc > >> it, send an email to amber-lang+ > ...@googlegroups.com <http://googlegroups.com> > >> <javascript:>. > >> For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout> > >> <https://groups.google.com/d/optout > <https://groups.google.com/d/optout>>. > > > > -- > > You received this message because you are subscribed to the Google > > Groups "amber-lang" group. > > To unsubscribe from this group and stop receiving emails from > it, send > > an email to [hidden email] <javascript:> > > <mailto:[hidden email] <javascript:>>. > > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. > > -- > You received this message because you are subscribed to the Google > Groups "amber-lang" group. > To unsubscribe from this group and stop receiv > an email to [hidden email] > <mailto:[hidden email]>. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
I finally got a handle on it. It turns out that all my problems were related to CORS (http://stackoverflow.com/questions/21850454/how-to-make-xmlhttprequest-cross-domain-withcredentials-http-authorization-cor). To bypass all this CORS shit, I decided to do my own user authorization. Works like a charm.
-- On Saturday, 30 May 2015 09:38:06 UTC-4, Herby wrote:
You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
Richard Eng wrote: > I finally got a handle on it. It turns out that all my problems were > related to CORS I am pretty sure that's not the case. > (http://stackoverflow.com/questions/21850454/how-to-make-xmlhttprequest-cross-domain-withcredentials-http-authorization-cor). > To bypass all this CORS shit, I decided to do my own user You cannot 'bypass' CORS (in fact, SOP) by any means. > authorization. Works like a charm. Though I am not going to argue - let it work until it works. -- You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
I didn't mean bypass CORS itself, just bypass the complexity of dealing with preflighted requests and OPTIONS headers. I had to accommodate CORS in the REST api server, so I didn't actually bypass it.
-- To apply user authorization for accessing the REST api, it's simply a matter of POSTing the credentials via SSL and doing the checking in the server-side code. AFAIK, this is perfectly secure. On Sunday, 31 May 2015 11:09:56 UTC-4, Herby wrote:
You received this message because you are subscribed to the Google Groups "amber-lang" group. To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email]. For more options, visit https://groups.google.com/d/optout. |
Free forum by Nabble | Edit this page |