[Proto-Scripty] Re: ajax request no works on Chrome - decoding fails
Hi, I tried use fixedEncodeURI like suggest in your links. I've never even heard of fixedEncodeURI. People are taking the time to help you here, it's worth taking the time to carefully *read* what people have written and try the things they suggest (or not, of course, it's up to you). This is what I've suggested that you haven't, as far as I can tell, tried: On Nov 8, 9:42 pm, T.J. Crowder t...@crowdersoftware.com wrote: The most reliable way to send parameters that I know is to send them URL-encoded, and to decode them as URL-encoded data. In Prototype, the easiest way to do that is to supply a plain object to the Ajax.Request method (which Prototype will correctly encode for you): var jsonRequest = ...; new Ajax.Request( // ... parameters: {json: jsonRequest} // ... }); ...and then retrieve the value just as you would any other value: $request = $_POST[json]; $requestObject = Zend_Json::decode($request); // Or your $zendJson, whatever that is Good luck with it, -- T.J. On Nov 9, 11:37 pm, fashionpeople fashionpeople.busin...@gmail.com wrote: I tried use fixedEncodeURI like suggest in your links. But doesn't work. I didn't understand the problem. On 9 Nov, 17:25, T.J. Crowder t...@crowdersoftware.com wrote: Hi, I replaced escape with encodeURI. But decoding fails again. Well, did you try actually doing what I suggested? -- T.J. On Nov 9, 4:07 pm, fashionpeople fashionpeople.busin...@gmail.com wrote: Hi, I replaced escape with encodeURI. But decoding fails again. function sendMessage(baseUrl, idNickRcv, msg) { var requestObject = new Object(); requestObject.idNickRcv = idNickRcv; requestObject.msg = msg; var jsonRequest = JSON.stringify(requestObject); if ((idNickRcv) (msg)) { new Ajax.Request(baseUrl + '/usermsg/index/sendmessage', { method: 'POST', requestHeaders:{ Accept:'application/json' }, parameters: encodeURI(jsonRequest), onSuccess: function(transport, json) { //use and handle foo response data } , on500: function(transport) { //handle error, inform user }, onComplete: parseSendMessage }); } php: $request = rawurldecode($this-getRequest()- getRawBody()); // HERE DECODING FAILS AND EXECUTION IS INTERRUPTED. $requestObject = $zendJson-decode($request, Zend_Json::TYPE_OBJECT); $msg = $requestObject-msg; $idNickRcv = $requestObject-idNickRcv; On 8 Nov, 23:11, Walter Lee Davis wa...@wdstudio.com wrote: Right. The best low-level way to translate JS to PHP and back again is using encodeURI or uncodeURIComponent on your JS side, and rawurldecode() or rawurlencode() on the PHP side. They are functionally identical, as long as you have set your PHP side to use UTF-8 as its default charset. Walter On Nov 8, 2010, at 4:42 PM, T.J. Crowder wrote: Hi, I don't know that it's the problem because I'm not a PHP person, but you're using the `escape` function to encode your parameters, and then decoding them with a PHP function called `urldecode`. JavaScript's `escape` function does _not_ URL-encode things, it does something similar but different and is almost certainly not what you want. I'm surprised it's working with other browsers, frankly, but perhaps that's my lack of PHP knowledge. The most reliable way to send parameters that I know is to send them URL-encoded, and to decode them as URL-encoded data. In Prototype, the easiest way to do that is to supply a plain object to the Ajax.Request method (which Prototype will correctly encode for you): var jsonRequest = ...; new Ajax.Request( // ... parameters: {json: jsonRequest} // ... }); ...and then retrieve the value just as you would any other value: $request = $_POST[json]; $requestObject = Zend_Json::decode($request); // Or your $zendJson, whatever that is But again, I'm not a PHP guy and could easily be missing something important here. FWIW, -- T.J. Crowder Independent Software Engineer tj / crowder software / com www / crowder software / com On Nov 8, 5:47 pm, fashionpeople fashionpeople.busin...@gmail.com wrote: Hi, this is my ajax request that works perfectly on IE and FIREFOX, but not in CHROME! function sendMessage(baseUrl, idNickRcv, msg) { var requestObject = new
[Proto-Scripty] Re: ajax request no works on Chrome - decoding fails
It may be helping if you could trace and post here the following things : From Javascript: - requestObject - JSON.stringify(requestObject) From PHP: - $this-getRequest()-getRawBody(); - rawurldecode($this-getRequest()-getRawBody()); Also, why do you need to use JSON.stringify thing when, as T.J. told you (twice) prototype is designed to take care of it for you. Eric On 10 nov, 00:37, fashionpeople fashionpeople.busin...@gmail.com wrote: I tried use fixedEncodeURI like suggest in your links. But doesn't work. I didn't understand the problem. On 9 Nov, 17:25, T.J. Crowder t...@crowdersoftware.com wrote: Hi, I replaced escape with encodeURI. But decoding fails again. Well, did you try actually doing what I suggested? -- T.J. On Nov 9, 4:07 pm, fashionpeople fashionpeople.busin...@gmail.com wrote: Hi, I replaced escape with encodeURI. But decoding fails again. function sendMessage(baseUrl, idNickRcv, msg) { var requestObject = new Object(); requestObject.idNickRcv = idNickRcv; requestObject.msg = msg; var jsonRequest = JSON.stringify(requestObject); if ((idNickRcv) (msg)) { new Ajax.Request(baseUrl + '/usermsg/index/sendmessage', { method: 'POST', requestHeaders:{ Accept:'application/json' }, parameters: encodeURI(jsonRequest), onSuccess: function(transport, json) { //use and handle foo response data } , on500: function(transport) { //handle error, inform user }, onComplete: parseSendMessage }); } php: $request = rawurldecode($this-getRequest()- getRawBody()); // HERE DECODING FAILS AND EXECUTION IS INTERRUPTED. $requestObject = $zendJson-decode($request, Zend_Json::TYPE_OBJECT); $msg = $requestObject-msg; $idNickRcv = $requestObject-idNickRcv; On 8 Nov, 23:11, Walter Lee Davis wa...@wdstudio.com wrote: Right. The best low-level way to translate JS to PHP and back again is using encodeURI or uncodeURIComponent on your JS side, and rawurldecode() or rawurlencode() on the PHP side. They are functionally identical, as long as you have set your PHP side to use UTF-8 as its default charset. Walter On Nov 8, 2010, at 4:42 PM, T.J. Crowder wrote: Hi, I don't know that it's the problem because I'm not a PHP person, but you're using the `escape` function to encode your parameters, and then decoding them with a PHP function called `urldecode`. JavaScript's `escape` function does _not_ URL-encode things, it does something similar but different and is almost certainly not what you want. I'm surprised it's working with other browsers, frankly, but perhaps that's my lack of PHP knowledge. The most reliable way to send parameters that I know is to send them URL-encoded, and to decode them as URL-encoded data. In Prototype, the easiest way to do that is to supply a plain object to the Ajax.Request method (which Prototype will correctly encode for you): var jsonRequest = ...; new Ajax.Request( // ... parameters: {json: jsonRequest} // ... }); ...and then retrieve the value just as you would any other value: $request = $_POST[json]; $requestObject = Zend_Json::decode($request); // Or your $zendJson, whatever that is But again, I'm not a PHP guy and could easily be missing something important here. FWIW, -- T.J. Crowder Independent Software Engineer tj / crowder software / com www / crowder software / com On Nov 8, 5:47 pm, fashionpeople fashionpeople.busin...@gmail.com wrote: Hi, this is my ajax request that works perfectly on IE and FIREFOX, but not in CHROME! function sendMessage(baseUrl, idNickRcv, msg) { var requestObject = new Object(); requestObject.idNickRcv = idNickRcv; requestObject.msg = msg; var jsonRequest = JSON.stringify(requestObject); if ((idNickRcv) (msg)) { new Ajax.Request(baseUrl + '/usermsg/index/sendmessage', { method: 'POST', requestHeaders:{ Accept:'application/json' }, parameters: escape(jsonRequest), onSuccess: function(transport, json) { //use and handle foo response data } ,
Re: [Proto-Scripty] Re: transfert parameter (this) in Ajax.Request
Hi, Thank, Laurent 2010/11/3 T.J. Crowder t...@crowdersoftware.com Hi, If the function show_message is in new Ajax.Request() , the result corresponds to the Request. But if the function show_message is outside new Ajax.Request() then the result corresponds to the previous Request. I don't understand why ? It's because Ajax requests are *asynchronous* (by default). Let's take an example of a request that returns a plain text response which will be SUCCESS:data on success, or ERROR on error: function foo(url) { var data; new Ajax.Request(url, { onSuccess: function(response) { if (response.responseText.substring(0, 8) === SUCCESS:) { data = response.responseText.substring(8); } else { data = (could not get data); } } }); alert(data); // == WRONG, always alerts 'undefined' } What happens when we call `foo` in the above is: 1. A new request is created and started. 2. `alert` is called, showing the current value of `data` which is, of course, `undefined` because nothing has ever been assigned to it. 3. `foo` returns. 4. At some point in the future, the request completes and sets the `data` variable. Nothing ever does anything with it, though. Compare with: function foo(url) { var result; new Ajax.Request(url, { onSuccess: function(response) { if (response.responseText.substring(0, 8) === SUCCESS:) { data = response.responseText.substring(8); } else { data = (could not get data); } alert(data); } }); } Now here's what happens when we call `foo`: 1. A new request is created and started. 2. `foo` returns. 3. At some point in the future, the request completes and sets the `data` variable. 4. `alert` is called, showing the current value of `data` (which is what we just assigned to it). So what do you do about it? Put your processing of the result in the success callback (as above), rather than in the code that's starting the Ajax request. Sometimes this means that you have to provide callbacks to code calling your function. For instance, let's assume that `foo` is supposed to *return* `data` to the caller rather than using `alert`. We now know that `foo` can't do that with a return value -- because `foo` returns before the Ajax call is complete. So how would `foo` pass the information back to the caller? Via a callback: function foo(url, callback) { var data; new Ajax.Request(url, { onSuccess: function(response) { if (response.responseText.substring(0, 8) === SUCCESS:) { data = response.responseText.substring(8); } else { data = null; } }, onComplete: function() { callback(data); } }); } Here's what happens when we call this version of `foo`: 1. We start a new Ajax request. 2. `foo` returns. 3. At some point in the future, the Ajax call completes. If it's successful at the HTTP level (e.g., the response code is 2xx), Prototype will call the `onSuccess` handler. Our handler checks the response text to see if the call worked at the business logic level (remember our values are SUCCESS:data or ERROR), and if it was successful we set `data` to the data from the string; if it wasn't, we set `data` to `null`. 4. When the Ajax call completes (regardless of whether it succeeds or fails at the HTTP level), Prototype calls the `onComplete` handler. We call the callback function the caller provided, passing in `data`. If the Ajax request failed at the HTTP level (404, 500), we'll have never set `data` *at all* and so it will be `undefined`. If the Ajax request succeeded at the HTTP level but failed at the business logic level, we set `data` to `null` in the `onSuccess` handler and so it will be `null`. If everything worked, `data` will be the data from the response. So the documentation of our `foo` function would say that the callback will *always* be called back, and will be called back with one of three values: * undefined: The Ajax call failed at the HTTP level * null: The Ajax call succeeded but the server returned an error * a string: The call was successful and this is the data retrieved Now, you probably wouldn't design an API like that (I wouldn't), but I wanted to cover the various ways that `data` is or isn't populated. I'd probably design the API so that the callback received an object with properties saying what happened, something vaguely like this: function foo(url, callback) { var result; result = { success: false, message: unknown HTTP error, data:undefined }; new Ajax.Request(url, { onSuccess: function(response) { var text = response.responseText || ; if
[Proto-Scripty] Re: ajax request no works on Chrome - decoding fails
J. Crowder do not get me wrong, I thank you for the effort they put in helping others. i readed about your suggest to send data in this simple way: var jsonRequest = ...; new Ajax.Request( // ... parameters: {json: jsonRequest} // ... }); but my question is, i have all based application such statements: var requestObject = new Object(); requestObject.idNickRcv = idNickRcv; requestObject.msg = msg; i want convert requestObject in JSON string and send to the server. var jsonRequest = encodeURIComponent(JSON.stringify(requestObject)); new Ajax.Request(baseUrl + '/usermsg/index/sendmessage', { method: 'POST', requestHeaders: {Accept: 'application/json'}, parameters: jsonRequest, //... In this way works only IE and FireWorks. Not on Chrome always decoding fails. you must use stringify? scriptaculous encodes them alone? Thank you -- You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptacul...@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
[Proto-Scripty] Prototype 1.7rc3 bug with Chrome sending ajax request twice and Opera even 3 times
I'm using the latest RC3. Chrome sends ajax request twice, and Opera - even 3 times. Chrome 6.0.742.63, Opera 10.63 - both on ubuntu linux, but on windows results are the same. Here's code: ... this.options.comm_send.observe('click', this.sendComment.bindAsEventListener(this)); //binding onclick to button ... sendComment: function(e) { new Ajax.Request(this.url, { parameters: this.options.comm_send_form.serialize(true), onComplete: function(transport){ var resp = transport.responseJSON; ... }.bind(this) }); } ... -- You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptacul...@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.