In DNN, how to get base URL in Javascript Files - dotnetnuke

The Active Forums module is making AJAX calls to it's API but it doesn't work properly if the site is running in a sub-directory. How should it get the BaseURL that includes any subdirectories? (e.g example.com/intranet)
Sample call from a JS file (jquery.afFileUpload.js):
$.ajax({
type: "GET",
url: '/DesktopModules/ActiveForums/API/ForumService/GetUserFileUrl?FileId=' + fileid,
beforeSend: sf.setModuleHeaders,
})
I'm sure DNN has a proper way to get the Base URL (i.e. example.com/intranet) or to make the call in a way that handles this properly.
More info: https://github.com/ActiveForums/ActiveForums/issues/317

url: window.location.hostname + '/DesktopModules/ActiveForums/...'
Happy DNNing!
Michael

Related

CSRF token mismatch in post request in 3.6 version

I have two different apps of cakephp. One has a version 3.5 and other 3.6.
When i used and built 3.5 app i did not have a problem of CSRF matching in post request. But now as i am using 3.6 it is giving me error of CSRF token.
Although in both app's AppController, CSRF component is disable.
//$this->loadComponent('Csrf');
i am using simple post request like this:
$.ajax({
type: "POST",
url: "../user/my_action",
dataType: 'json',
success: function (data) {
set_data(data.response);
}
});
What am i missing? or some configuration i have done wrong?
The latest 3.6 application template now uses the CSRF middleware by default, see your apps src/Application.php file. Unfortunately this change hasn't been documented properly, and hit people by surprise.
Ideally you do not disable it, but instead pass the proper CSRF token alongside with your AJAX requests, you can easily obtain the token from the request object in your view templates, for example in the layout to make it globally available:
<script>
var csrfToken = <?= json_encode($this->request->getParam('_csrfToken')) ?>;
// ...
</script>
You can then easily use it in your AJAX requests:
$.ajax({
headers: {
'X-CSRF-Token': csrfToken
},
// ...
});
See also
Cookbook > Middleware > Cross Site Request Forgery (CSRF) Middleware
Cookbook > Middleware > CSRF Protection and AJAX Requests
Add this code on your $.ajax() function call:
beforeSend: function (xhr) { // Add this line
xhr.setRequestHeader('X-CSRF-Token', $('[name="_csrfToken"]').val());
}, // Add this line
For CakePHP 3.8, this worked for me. In config\routes.php
comment the line as so :
//$routes->applyMiddleware('csrf');
i had the same error, without using ajax, the problem was that admin theme was not using cakephp 3 form helper sintax, it was html.
after i changed with Form->create() ?> Form->end() ?> it worked fine

Angular & Ionic, $http not working in real device Android

I am working on ionic application and using $http for get the data from web service. I have tested and its working on browser perfectly but i don't know why its not working on my android mobile, it just keep processing and after 3 to 5 mins it shows:
ionic.bundle.js:25000 POST http://xyz-domain.com/api/ net::ERR_CONNECTION_TIMED_OUT
Even i have installed cordova-plugin-whitelist plugin but still getting same.
My Code:
var ApiURL = 'http://xyz-domain.com/api/';
$http({
url: ApiURL,
method: 'POST',
data: 'action=activate&app_secret=123456',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(res){
alert(res);
});
Can any one please tell me how i can solve this issue ?
Thanks
This might be the case that your white list plugin and android platform version are not compatible.Happened to me also.Updating the platform and the plugin resolved the issue.Also can you alert the data, status, headers, config that are returned by the service.

AngularJS [Ionic] - Google apis XMLHttpRequest cannot load

I would like get the json data from Google Api but with get method and jsonp method this is not work.
Her is my code :
$http({
method: 'get',
url: 'https://maps.googleapis.com/maps/api/place/search/json?location=46.2030350,6.1522080&radius=500&types=restaurant&sensor=false&key=MY_KEY&callback=?'
}).then(function successCallback(response,data) {
console.log(data)
}, function errorCallback(response) {
console.log('nope')
});
I'have XMLHttpRequest cannot load.
This is mostly like you are trying to run the file without a server directly via browser. You would need either xampp or other webserver.
OR just install python 2.7 and use the following command from where your project files are location python -m SimpleHTTPServer 8000 and then navigate to localhost:8000

In Angular project reference to php script give 404 (Not Found) locally, but works on live site

I'm new to Angular and I'm using Yeoman for scaffolding and Grunt for builds. I'm attempting a post to a php script with the following code:
$http({
method : 'POST',
url : '/components/contact/test.php',
data : $.param($scope.user), // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
});
When I run this on my local server I get the following error
POST http://localhost:9000/components/contact/test.php 404 (Not Found)
How ever when I do a build and push to my web site it does find the php script (I have checked and it is there locally). I tried a number of different variations on the file path and none seem to work. Can anyone explain what's going on here?

angularJS $http.get() success callback not be called on cross domain

I am developing a small application use angularJS. The html pages are local file which will not deploy on web server. I defined a service module which will call the remote webapi to get the json data, however my success callback not be invoked.
$http({ method: 'GET', url: remoteServiceUri }).
success(function (data, status) {
var response = data;
}).
error(function (data, status) {
var error = data;
});
it always call into the error method. how can I resolve this issue please?
I can confirm that the service api work fine, as I tried deploy the page and webapi on the same site, in this case, it works.
is this caused by the cross domain or any configuration required?
Thanks.
You need to configure your remote web service to handle the preflight OPTIONS request.
Your web service must add the following headers to the response of the preflight OPTIONS request as well as the actual request:
{
"Access-Control-Allow-Origin": "*",
"Access-Control-Expose-Headers": "Content-Type",
"Access-Control-Allow-Methods": "GET,POST,PUT,DELETE,OPTIONS",
"Access-Control-Allow-Headers": "Content-Type"
}
You can find details about what which headers are required, and what do they mean here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
If you are using Apache, you could use proxypass to redirect requests. This way your angular app talks "locally" with your apache server, and it will pass the request to a different domain.
For example, in your httpd.conf set:
ProxyPass /foo http://foo.example.com/bar
In your angular app call
$http({ method: 'GET', url: "/foo/action" }).then(...);
Your apache will translate "/foo/action" to "http://foo.example.com/bar/action".
Bye bye cross domain issues!
For more info, see apache proxy module

Resources