MVC Deployment - Relative Path issue with respect to Application Path - javascript

While deploying my MVC Project, I have faced an issue of relative path w.r.t server.
I am hosting the project as an application in IIS. Finally the url to my application will look like http://localhost/portal/Account/Login here 'portal' is the application name in IIS. In the ASP.net development server everything was working fine. while deploying it needed the relative path with respect to server. Because of this my jquery ajax requests started failing.
To fix this issue I kept the actions in hidden field and accessing from there and using for ajax request. The following is the code.
<input type="hidden" value="#Url.Action("GetNewOffersSearch", "Updates")" id="NewOffersSearchUrl" />
var NewoffersUrl = document.getElementById("NewOffersSearchUrl").value;
$.ajax({
type: 'GET',
url: NewoffersUrl ,
cache: false,
timeout: 10000,
contentType: "application/json; charset=utf-8",
success: function (_results) {
$("#updatesContent").html(_results);
},
error: function (_results) {
}
});
Initially NewoffersUrl was "/Updates/GetNewOffersSearch" and it was throwing a path error. But now it is "/portal/Updates/GetNewOffersSearch" and its working fine
I just want to know the approach I am following is correct or not. Is there any better fixes for this issue?

The way we do AJAX requests is similiar, however we pass the URL directly to the url parameter of the ajax call rather than using a hidden field.
$.ajax({
type: 'GET',
url: #Url.Action("GetNewOffersSearch", "Updates"),
cache: false,
timeout: 10000,
contentType: "application/json; charset=utf-8",
success: function (_results) {
$("#updatesContent").html(_results);
},
error: function (_results) {
}
});

Related

Route not accessible from ajax function in a seperate js file in online server

I have a laravel project. Each view page has it's on seperate js file. Problem is I am not able to access route functions from ajax post or get call in online server(digital ocean).
Error is:
Failed to load resource: the server responded with a status of 404 (Not Found)
The same application is working fine in my local system.
When I add the ajax function inside the design (blade.php) page it's working fine in server. It's not so pleasing to change all my functions inside design.
The server and application was working fine till yesterday. I made some changes in design and pulled it from bitbucket to server today. After that this error occurred.
Is there anything I have to do in server after pulling from bitbucket. I only pulled the changes made in design.
My route function:
Route::prefix('invoice_vno_bind')->group(function () {
Route::get('/', 'InvoiceController#getnextvno')->name('invoice_vno_bind');
});
AJAX function:
$.ajax({
type: "GET",
url: "invoice_vno_bind",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
$('#txtVNo').val(data);
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.responseText);
}
});
Error function of ajax is executed when running in online server.
First, blade.php in add hidden parameter.
<input type="hidden" name="ajax_url" id="ajax_url" value="{{ route('invoice_vno_bind') }}">
And then get id="ajax_url" value in js file.
var url = $('#ajax_url').val();
$.ajax({
type: "GET",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
$('#txtVNo').val(data);
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.responseText);
}
});
Finally I got the error. It was because of some program lines in
/etc/nginx/sites-enabled/{your file name}
I commented out the codes below :
# location ~ /.(?!well-known).* {
# allow all;
# }
}
After commenting it worked fine. I don't know if this will affect any other fields. Till now it's perfectly working.
Thank you all for your suggestions.

IIS URL Rewrite and ajax calls in Asp.NET Core 2.2

I have a Asp.NET Core 2.2 site at localhost:81 and I have set up a IIS URL Rewrite to make it accessible from
www.mydomain.com/mysite
Note: I have other sites located on other ports on localhost which are published the same way.
The URL Rewriter takes care of rewriting the references in .cshtml files, but it doesn't handle my ajax calls:
$.ajax({
type: "GET",
url: "/Customers/Index?handler=HairColor&customerID=#Model.CustomerID&date=" + myDate,
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: null,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {},
failure: function(response) {}
});
I don't know how the rewrite module could ever know that it should rewrite that specific url in my javascript.
Question:
What is the right way to make the ajax call compatible with my website setup?
I am starting to wonder if hosting multiple sites like this on a single domain
is the wrong approach?

Cross Origin in ajax not working for .properties file in IOS (10.3.1)

I used i18n plugin for load *.properties file for translation and its working fine on android platform but same library not working on IOS 10.3.1. It gives me below error:
i have done some changes in i18n library but still its not working.
function loadAndParseFile(filename, settings) {
$.ajax({
url: filename,
async: false,
cache: settings.cache,
crossDomain: true,
jsonpCallback: 'callback',
contentType: 'text/plain;charset=' + settings.encoding,
dataType: 'text',
success: function (data, status) {
parseData(data, settings.mode);
}
});
}
In above code:
i have been added Cross-Domain 'true' and datatype 'text'.. when i changed datatype 'text' to 'jsonp' its working but it gives .properties file error.
Please check below error..
That means. file is loaded, but inner data format is different.
If you are using now JSONP instead of text, the file will be loaded as javascript code, so if contents are not valid javascript code it will fail.
Surround data with a global variable assignation or a function call:
window.variable = "_DATA_"; // or
functionName("_DATA_");
If _DATA_ are JSON format, then you don't need surround with quotes, otherwise you'll need to use "_DATA_" because without quotes it will not be valid javascript syntax.
add this line to your ajax parameters
xhrFields: {
withCredentials: true
}

Use Server.MapPath in jQuery AJAX call while deployed on local IIS

I am using WebMethod on my ASPXpage and I call it from jQuery on the same page like this:
$.ajax({
type: "POST",
url: "WebForm1.aspx/Saveuser",
contentType: "application/json; charset=utf-8",
data: parameters,
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
This works fine in my debug environment, but when I deploy on the hosting site it does not. The problem seems to be in the URL because on the server the path would be different. So I used Server.MapPath in various ways but none of them worked.
Url: '<%= Server.MapPath("~/MyPage.aspx/GetSomeData")%>'
Can anyone overcome this defect?

Unable to perform a Cross domain ajax call. - JQuery

I have checked many post and tried every logic that were mentioned in various blogs and posts. But I am unable to perform a cross domain ajax call to an IIS server. Please anybody advice what else I should look into or configure it to get working. All your help is greatly appreciated.
Here is my ajax call:
var url = "http://mydomain .com/myauthorizeservice";
var jsonParam = JSON.stringify({ username: 'user007', password: 'pass007' });
$.ajax({
type: "POST",
url: url,
crossDomain: true,
data: jsonParam,
success: fnSuccess,
error: fnError,
dataType: "json",
contentType: "application/json"
});
function fnSuccess() {
alert("Success");
}
function fnError() {
alert("Error");
}
My config in the root web.config:-
Error:-
Access Denied.
I really struggled a lot to make this thing work. Here some points that also matters and restrict the cross domain calls-
Note: I am using WCF REST service. and configurations are for IIS 7.5.
1: Make sure your OPTIONSVerbHandler looks like-
2: Make sure you have the correct ordering in HandlerMappings-
Rest settings and the way to perform ajax call is mentioned in the question.
Happy coding!

Categories