How can we GET/POST the request to server with scriptaculous.js or prototype.js - scriptaculous

How can we GET/POST the request to server with scriptaculous.js or prototype.js.
Please explain with brief example if possible..
Regards,
Akash Jain

You ca use Ajax.Request to send Ajax requests to your server (must be on the same domain name). For instance (quoting the doc) :
new Ajax.Request('/some_url', {
method: 'get',
parameters: {company: 'example', limit: 12}
});
And, for POST, replace 'get' by 'post' ;-)
See :
Introduction to Ajax
Ajax.Request
Note that this can be used only to send request to a script on your own domainname, because of the Same Origin Policy implemented in web browsers for security reasons.
If you want to send requests to another domain, you'll have to go through a proxy installed on your own (so the request seems to be sent to your domain).
Scriptaculous is an "effects" framework, to do stuff like animations, drag'n drop, and that kinf of things.
It uses Prototype, but doesn't provide any Ajax-requesting functionnaly : it only uses those of Prototype, when necessary.

Here it is. It's a common usecase and thus very prominent in the documentation for Prototype.
Introduction to Ajax
http://www.prototypejs.org/learn/introduction-to-ajax

Related

Run a flow from another flow in Twilio

How can I run a flow from another flow in Twilio Studio Flow?
Help with defining the To and From HTTP parameters:
I am a beginner in programming so I am failing to understand the brief notes given in support docs, namely specifying HTTP additional parameters for "To" and "From".
Additional details from comment:
I am trying to run REST API triggered Flow B from primary Flow A by using an http request widget in Flow A in the format below: (as suggested in a similar problem posted on this portal) Widget: HTTP Request [ACCOUNT_SID:AUTH_TOKEN#studio.twilio.com/v1/Flows/THE_OTHER_STUDIO_FLOW_SID/Executions][2] Content Type: Form URL Encoded KEY:VALUES To:+1234567890 From:+2773123456 I am getting error 401. I tried to swap the To number with the From number without success
There are 2 ways you can trigger one twilio studio flow from another
Method 1:
Use the TwiML Redirect Widget. Place the widget where you need it and specify the target studio flow URL there. Studio URLs have the following format
https://webhooks.twilio.com/v1/Accounts/{AccountSid}/Flows/{FlowSid}
Method 2:
Do the same as above programmatically. You can send twilio a twiML response such as the one below
let twiml = new Twilio.twiml.VoiceResponse();
if (something) {
twiml.redirect({
method: 'POST'
}, 'https://webhooks.twilio.com/v1/Accounts/{AccountSid}/Flows/{FlowSid1}');
} else {
twiml.redirect({
method: 'POST'
}, 'https://webhooks.twilio.com/v1/Accounts/{AccountSid}/Flows/{FlowSid2}');
}
For more info, check out https://www.twilio.com/docs/voice/twiml/redirect
Assuming you are not trying to bridge the call between the two flows, this should be possible. To simplify:
You have a call come in on Flow A ("Incoming Call" trigger on Flow A).
Flow A executes its logic.
That logic triggers Flow B by calling its REST API endpoint so that it makes a new outbound call ("REST API" trigger on Flow B).
This last thing is the hard part. Make sure you are looking at the docs for the REST API Execution resource. To trigger a new flow, you need to make a POST request which supplies the To and From parameters.
If you are a beginner at programming, it might be helpful for you to start with a separate HTTP client like Postman to start to get familiar with the structure of an HTTP request, and learn the full extent of what is required to successfully make this API request before you start trying to cram it into Studio and automate it.
That said, this request should be possible to do within the Studio Make HTTP Request widget. If you make your content type Application/JSON, you can pass the To/From parameters directly in a JSON-formatted request body, like this:
{
"To": "+19995551234",
"From": "+12345556789"
}
To be perfectly honest, I don't know what the widget means by "Http Parameters". This could be HTTP Headers, URI parameters, or something else. I think the JSON form is clearer.
I came across the same situation. The solution for authentication is to change the url to include AccountSid and AuthToken
https://[AccountSid]:[AuthToken]#studio.twilio.com/v2/Flows/[SID]/Executions
Instead of Application / Json, use Form Parameters. Then add individual parameters below, for To, From, and Parameters​ (JSON string) for other variables.

IE9 removes # part from URL (works on Firefox! )

I am working on an application with ASP.NET MVC Routing + AngularJS routing.
My URL lookslike:
https://example.com/Request/#/Search/Request/123
when I breakdown this (http://example.com/Request) is handled by ASP.NET MVC routing. i.e. (Area = Request, controller = "Default", action = "Index")
(#/Search/Request/123) is handled by AngularJS routing.
This works perfectly when I am on http://localhost:8080/
The issue is when I deploy this application to https://example.com/
In this case, If user clicks on above link (received via email),IE 9 recognizes only (https://example.com/Request/") and the server never gets (#/Search/Request/123).
We have enterprise SSO implemented on web server. SSO client intercepts http request and uses URL to redirect back to requested page after authentication.
if # fragment is not sent as part of http request url, sso is not able to redirect back to same page.
I believe this to be a common scenario/issue. I would keep changing the URL scheme as last resort. e.g. (# to !).
How to solve this?
Just found a blog that dealt with this issue exactly:
http://codetunnel.io/how-to-persist-url-hash-fragments-across-a-login-redirect/
He offers two ideas:
When the page loads there simply needs to be some JavaScript that accesses the hash fragment and appends it to the redirect URL in the hidden field. Here's an example using JQuery for simplicity
$(function () {
var $redirect = $('[name="redirect"]');
$redirect.val($redirect.val() + window.location.hash);
});
Or, alternatively
Instead of appending the hash fragment to the hidden field value, you could avoid sending it to the server at all and simply append it to the form action URL.
$(function () {
var $loginForm = $('#loginForm');
var actionUrl = $loginForm.attr('action');
$loginForm.attr('action', actionUrl + window.location.hash);
});
Fragments (the part of the URL after the #) are not necessarily sent to the server-side by the browser. They are for client-side usage only (navigating to a specific location in the document, JavaScript support).
RFC 2396 section 4.1:
When a URI reference is used to perform a retrieval action on the
identified resource, the optional fragment identifier, separated from
the URI by a crosshatch ("#") character, consists of additional
reference information to be interpreted by the user agent after the
retrieval action has been successfully completed. As such, it is not
part of a URI, but is often used in conjunction with a URI.
(emphasis added)
Therefore, the URL scheme you came up with will not work reliably unless you change the # to another character. Alternatively, you could use JavaScript to transfer the information from the fragment in an input that will be reliably passed back to the server. But do note that solution will only work if JavaScript is enabled in the browser, so it is (also) not a 100% reliable solution that will work with all clients.
Either way, using a URL without a fragment is a more reliable approach and IMO a better design choice if you expect that part to be interpreted by the server.
I would remove ugly URL's from your application all together.
This article will walk you through removing ugly URL's in a asp.net-mvc project. It will also ensure that you have your RouteConfig.cs setup correctly.
http://www.codeproject.com/Articles/806500/Getting-started-with-AngularJS-and-ASP-NET-MVC-P

How to set cache-control header in odata4j?

I'm writing a RESTful service using odata4j and need to be able to set the caching header of the response.
How do I do this?
I don't seem to have access to the HttpservletResponse object.
And unlike the JAX-RS support, I can't see anything in odata4j that lets me get hold of a CacheControl object.
Thanks
Sarah
No direct support for this as of 0.5 - however you could write a custom ContainerResponseFilter to modify the outgoing response manually.
Feel free to add a feature request on the project issue list [1], along with any detail on a proposed api if you have thoughts on how this might work.
Or, of course, a patch... : )
Hope that helps,
- john
[1] http://code.google.com/p/odata4j/issues/list

Internal data post [Kohana 3.1]

In Kohana 3.1.x framework.
What are the benefits to send data with internal requests like this
$post = Request::factory('module/data')
->method(Request::POST)
->post(array('some' => 'random data'))
->execute()
->response;
if you could simply send data like this
Module::instance()->data(array('some' => 'random data'));
In this example Module is a random module and data is some random method.
I'll call this Module via ajax and internal requests. I'm planning to design RESTful API.
QUESTION IS: Why use HMVC instead of just directly using an internal class API
Because they're internal requests, there is no additional HTTP request being made.
You might want to take a look at Request_Client_Internal and compare it to Request_Client_External. After that you should feel enlightened :)
Edit:
You should know that AJAX requests aren't the only "external HTTP requests". cURL, PECL HTTP, file_get_contents() and other PHP functions will also send an external HTTP request (imho you should read the RFC 2616 to understand how HTTP actually works).
With HMVC calls you can use the same controller for both Ajax and internal requests. Also, it can handle a standard (non-ajax) http-requests, form submits for example. All-in-one solution, single entry point.
If you dont want HMVC calls, you will require one call for internal request (somewhere in base controller) and another one - in a special Ajax controller. Also you may have a problems with a data rendering (usually HMVC and ajax calls are using different templates). Its not DRY.
I would comment on the above, what biakaveron said, but I can't yet, so I put it as an answer.
#stacknoob: Could you use Module::instance()->data(array(...)) as controller's action? You could - with some extra code.
Instead, what biakaveron already said, you can keep your code logic and have the action return the same result for AJAX and HMVC requests. In one place. DRY + KISS.

Circumventing browser same origin policy with a proxy in Rails 3 application

I'm looking for a rails solution that can consume multiple remote XML services, passing dynamic request parameters and outputting the response as XML or JSON.
I've looked into TinyProxy (Can't get it to install on OSX via macports) and also Nginx. Nginx looks like it will do what I need and also give us flexibility going forward with load balancing etc.
Has anyone else got any experience of this? Any tried and tested solutions?
In stead of going through a proxy, one of the standard solutions around the same-origin policy is dynamic script tags and JSON callbacks.
For example: you page page wants to query an API at remotesite.com and you try to do an ajax call to http://remotesite.com/api?query=list but you get the same-origin error. To circumvent the restriction you could add a script tag to the DOM (using JS) that points to the url like this:
var e = document.createElement('script');
e.src = 'http://remotesite.com/api?query=list';
document.getElementById('fb-root').appendChild(e);
The browser would then run that request - the same thing you tried to do w/ an ajax call. Now the catch is that you need to have the response call one of your js functions w/ the data returned as a argument. So the request would return something like:
callbackFunctionname({...json_data_here...});
Now in your code you'd have a function like this:
function callbackFunctionname(json_string)
{
//you have result from cross domain ajax request.
}

Resources