How to find current domain in PrototypeJS? - prototypejs

Sometimes the current domain is builds and at other times it's builds.example.com. I would like to call an AJAX request to the same domain as the current one (so that it doesn't violate the same origin policy). How do I find the current domain? If that's not possible, are there any other solutions to the problem?

There's no need to find the current domain. Just use a partial URL:
new Ajax.Request("/partial-url-without-domain", {
method: "post",
parameters: parameters
});

Related

Laravel Routes - Possible to reuse ajax route call from one form on different form?

I have an existing route I created to perform an ajax call for a chained select:
Route::get('add/{id}','AddAssetController#getModel');
The above queries the db for models belonging to each manufacturer.
The url part of the ajax call (man_ID is the id of the manufacturer from the select):
url: 'add/' + man_ID,
The above works perfectly on the main form where I am using it. However, I've found that I need this chained select on more than one form. Is there a way to be able to use the same route for two different pages? I've tried calling it from a different page and in the console, I'm getting a 404 error.
Am I missing something? Is this possible?
You are probably calling the script from the differently nested pages, thus add/ is ultimately calling different path.
I would strongly advise you name your routes, and generate links like that:
Route::get('add/{id}','AddAssetController#getModel')->name('get-model');
and in your blade file
url: '{{route('get-model', ['id' => $id])}}',
if you still set on using paths to call your script, use them relative to your root:
url: '/add/' + man_ID,

Securing web api using random session number

I am making a web API/Rest API in MVC where each API takes one parameter called user_session
So when a user logs in, I generate a 10 digit session and pass it back to user which needs to be given as input for any subsequent API calls.
Here's what my typical code looks like:
$.ajax({
url: '#Url.Action("GetUserDetail", "myapi")',
type: "GET",
data: { UserID: '#user.user_id', SessionID: '#user_session' },
dataType: "json",
success: function (response) {
}
})
My question is, is this the right approach or is there a better way of doing it? Is it secure?
You can very well use the HTTP Cookies for remembering the session instead of manually passing it as a param for every request.
I’ll give very short explanation on this topic.
Server sets a cookie when a user logs-in.
This cookie will be sent to all the api calls as it is defined as a HTTP cookie.
Server can validate or know about the session using this cookie value in header of the request.

Accessing the information in build.settings through an ajax request

So I know you can use json files to load data into a store, but I was wondering if there exists a way to make a request to only access a single "variable", in a similar way Ux.Locale.Manager works.
To be more specific, I'm working with an app built on Sencha Architect, and I would like one of the views to contain the version of the app. While I could just hardcode that label and update it every time I make a build, I was wondering if it was possible to just access the information in the build.settings file, specifically the versionString and versionCode variables, to make things easier? And if possible, how would I go about making an ajax request without involving a Model and a Store?
I figured it out. I kept thinking I had to make a call with an AjaxProxy, but a simple Ext.Ajax.request did the trick:
Ext.Ajax.request({
url: 'build.settings',
method: 'GET',
success: function(response, opts){
var data = JSON.parse(response.responseText);
Ext.getCmp('appVersion').setHtml("version " + data.androidBuild.versionString + "." + data.androidBuild.versionCode);
}
});

Symfony, action accessible by forward only

Is it possible to prevent direct access to an action in symfony. The action is only accessible by "forward" only. So basically, a way to see if the request is coming from another action.
I'm trying to achieve this because the first action handles plenty of verifications then if it fails, it stays on that action. If it succeed, it will forward to an appropriate action; this action needs to have safe inputs (validated from the first action). In order to keep the code DRY, the second action doesn't need to re-verify all the inputs again.
Then why not doing simply a private method? The second action is sort of a plugin, it's decided on the fly where it's going from the first one, that action has its own set of other future action/template. It makes more sense to simply forward instead of trying to handle plenty of cases that Symfony already takes care of.
There are multiple ways to achieve this.
1) Make sure your action isn't accessible by the routing. If you have wildcard routes this will be harder, but you can always add a route which would point the url for your action to a 404 page. Add something like this to your routing.yml:
disabled_action:
url: /disabledController/disabledAction
params: { module: default, action: error404 }
2) Check the action stack upon executing your action. The action stack let's you know from which action you were redirected. You can access it within your action using $this->getController()->getActionStack(). If the getSize() is bigger than 1 (in a default configuration) you we're forwarded.
Use referrer parameter available in request
$request->getReferer() will give you full url of previous action
I'm curious why you're trying to achieve this. Are you looking to have multiple points of access that forward to this action? What if you simply defined a private method (which by default aren't web-accessible) and called it directly from another action?

CodeIgniter routing problem. (appends ajax route to existing url)

I'm trying to perform an AJAX-request in a view, the user gives some input which is sent to the server with AJAX and the function it's supposed to go to is routed with CodeIgniters routes.
This is the view I'm currently standing in while making the request.
http://localhost:8888/companies/list
In my route config I've set this route below to handle the AJAX-request, which should be able to come from any view and still be able to go to the route I've specified.
$route['test_ajax'] = "ajax/test_ajax";
So the request should go to the "ajax"-controller and use the function "test_ajax", which should make the POST-url look like this.
POST http://localhost:8888/test_ajax
But instead what I get is the current URL I'm standing at, and the route I've specified appended to the URL crashing my response from the AJAX-request completely since it didn't even go close to the function it's supposed to. The POST-url I get looks like this.
POST http://localhost:8888/companies/test_ajax
Notice how the parameter of /companies was removed. The argument /list was lost somewhere, al though if I add a trailing slash after the list I get the list argument in the URL as well.
So what just happened is that the POST tries to go to the companies-controller and look for the function test_ajax which is defined in the ajax-controller and not in the companies-controller. This error keeps occuring no matter what URL I'm at, and it always follows the same pattern. It keeps appending my route-URL to the existing URL instead of routing correctly.
So what could be causing the routing to behave this way, is there any setting that's accidently enabled or anything? Because I know I've got this to work hundreds of times in previous projects.
Thanks in advance.
It is because your Javascript is using the current directory as the base, and appending the AJAX URL to it. Because you are (to the client-side at least) in the companies directory, it appends your URL onto this.
The solution, if your Javascript is inline, is to just use the base_url() PHP function wihtin the code ...
var url = '<?= base_url(); ?>test_ajax/'
If your Javascript is not inline, you can declare a global variable at the top of your HTML document using the PHP function...
var BASE_URL = '<?= base_url(); ?>'
And use it everywhere else in your Javascript ...
var url = BASE_URL + 'test_ajax/'
Alternatively, you could just hardcode your base URL, but that could get real messy real quick.
Turns out, CodeIgniter interpreted this as a relative link due to the fact that there was no heading slash. CodeIgniter User-Guide states that no heading or trailing slashes should be written in the routes config.
What solved this though was adding a heading slash in the java-URL.
$.ajax({
url: "/test_ajax",
type: "POST",
data: data,
success: function(data){
console.log(data);
}
});
This forces CI to interpret this as a absolute URL and gives me the URL I was looking for.
POST http://localhost:8888/test_ajax

Resources