How to do conditional branch to POST or GET depending on url? - post

I want to implement a conditional branch to POST or GET depending on path of url as below.
app.use(function(req, res, next){
var method = req.method;
switch(method)
{
case 'POST':
// I want to call a function() which has the same as a router, app.post()
Does anyone know how to do this?
Thanks.

Related

Still having ddl redirect issues

I am still having issues. I think what I am trying to do is simple. I have a viewbag droplist - which is working fine. I just want to redirect on selection it redirect just like: #Html.ActionLink("Branch", "Employees", new { Branch = item.Branch }) | (but with the selection from the droplist) Should this be difficult?
#Html.DropDownList("Branches", ViewBag.Branches as SelectList, "Select a Branch", new { #id = "ddlBranch" })
<script>
$(function () {
$("#ddlBranch").on("change", function () {
var deptId = $(this).val();
var routeVal = { Id: deptId };
var url = '#Url.Action("Department", "Home")';
$.ajax({
url: url,
type: 'POST',
data: routeVal
}).done(function (result) {
window.location.href = result.newUrl;
})
})
})
</script>
From a comment on the question above:
So now I just need to redirect to: #Html.ActionLink("Branch", "Employees", new { Branch = item.Branch }) but Item.Branch would be $(this).val(). Can this be done?
There may be a more elegant way, but an approach I've always taken is something like this:
let url = '#Html.ActionLink("Branch", "Employees", new { Branch = "REPLACEME" })';
url = url.replace('REPLACEME', $(this).val());
window.location.href = url;
On the client-side this turns into something like:
let url = '/Brand/Employees?Branch=REPLACEME';
url = url.replace('REPLACEME', encodeURIComponent($(this).val()));
window.location.href = url;
Which would look silly to anybody examining only the client-side code, I'm sure. But since the Branch parameter might be in the query string or might be in the route, and since it's the ASP.NET MVC Framework's responsibility to manage that, generating the URL and putting the client-side value into the URL are two separate responsibilities here.
So we use the server-side code to generate the URL with a placeholder (can be any string, "REPLACEME" was an arbitrary choice) and then use the client-side code to dynamically replace that placeholder with the desired value.
This essentially replaces whatever you're trying to do with AJAX in the original question. Unless you need to invoke a server-side operation to get a result not known to the client-side code in order to build the URL, you can just omit the AJAX entirely and build the URL directly.

How to capture post request in aqueduct?

In channel.dart my router configuration is shown below. But I couldn’t set the content type to text;
router.route('/login/[:value]').link(() {
return new LoginController();
//..contentType = ContentType.TEXT;
});
Than in my custom controller I pass the post request to Future (shown below)
Future<RequestOrResponse> handle(Request request) async {
String _xRequestValue = request.toString();
And I get this;
print(xRequestValue); // GET /login/Q101::49785:_:x (1530612696990)
I can print the value as shown above comment. I need to get Q101::49785::x part in request. My question is that how to capture post request in Aqueduct?
It was very simple;
if (request.path.variables.containsKey('value')) {
_xRequestValue = (request.path.variables['value']).trim();
}

How can I set an input variable of Code (Zapier) as an intire JSON object from previous step (Webhook-GET)

One of my multi-steps Zaps has a Zapier.Webhook-GET as a step 2.
Step 3 is Zapier.RunScript-Javascript.
I can´t figure out a way to set up that intire JSON object resulted from step 2 as the input variable required for step 3. The list of options shows only children and nested fields, but I need to take the object from the root.
I don't believe Zapier will allow that, specifically.
Here's an alternative that may work perfectly: Put the GET in the step 3 script and use fetch!
Here's an example:
//Put in your url with querystring
var url = "https://somewhere.com/rest?para1=value1";
//Add the method and headers here
fetch(url, {method: "GET", headers: {"Content-Type": "application/json"}})
.then(function (response) {
console.log(response);
return response.json();
}).then(function (data) {
//This is the entire JSON. Put your code here
// Remember to do a callback! Do not set to "output"
console.log(data);
//This will return data as output
callback(null, data);
});
//Code here will execute BEFORE code within the then functions because it's asynchronus
See this link for more on fetch: https://github.com/bitinn/node-fetch/tree/32b60634434a63865ea3f79edb33d17e40876c9f#usage
Hope this helps!

Change prefix of URL in GET request

I'm making a get with a relative URL with YAHOO.util.Connect.asyncRequest.
Example:
var myurl = "newpagetotest?something="something");
var myCallback = {
success: function(data) {
console.log("success");
},
};
var transaction =
YAHOO.util.Connect.asyncRequest('GET', myurl, callback);
And this makes, for example, get to the
localhost:8080/share/page/newpagetotest
But if I want to change the "localhost:8080/share/page", i.e., the prefix of URL absolute. How can I change? It's possible? Or how can I get this prefix for example, for one variable?
Thanks in advance.
I made this by using location.pathname.
It solves the problem.

Force jQuery.load() to POST when using jQuery.param()

Ok, so .load() uses...
The POST method is used if data is
provided as an object; otherwise, GET
is assumed.
I have the following...
// an array of itemIds
var items = $selected.map(function() {
return $(this).find('.item').text();
}).get();
// post the data
$container.load(
_url,
$.param(data, true),
function(response, status, xhr) {
//...
}
);
The problem I have is that if I use $.param to serialise the data, it seems that GET is used.
If I don't use $.param then POST is used but I run into the problem again with the array not being serialised correctly and I don't receive the data in my controller.
Is there an easy way around this?
You can use jQuery.get() instead of .load():
$.get(_url, $.param(data, true), function(data) {
$container.html(data);
});
This will have the same effect as a call to load with parameters, but with a GET request instead of a POST request.

Resources