I am developing my first web app using angularjs , created a aspx page , added the controller. When I debug in local ASPX page is getting opened and Controller also working fine , but when I call the save function in angular . It is returning the status code as not authorized .
below is the call i have made
$http({
method: "POST",
url: "Login.aspx/SaveOrganizationName",
dataType: 'json',
data: { },
headers: { "Content-Type": "application/json" }
}).
Any suggestion, how to fix ?? I have set the project to run on IIS. as I'm not having enough idea on this , please explain me detailed
thanks
Related
In my ASP.NET MVC application I have a problem in calling WebAPI controller by ajax statement.
Here is my code:
$.ajax({
url: 'api/cartitems',
type: self.cartItem.id == null ? 'post' : 'put',
contentType: 'application/json',
data: ko.toJSON(data)
})
.done(self.successfulSave)
.fail(self.errorSave)
It produces an error 404 - file not found.
I've tested different possibilities and only one that works is using whole URL path.
$.ajax({
url: 'http://xx.yyy.zz.vvv/APP_NAME/api/cartitems',
type: self.cartItem.id == null ? 'post' : 'put',
contentType: 'application/json',
data: ko.toJSON(data)
})
.done(self.successfulSave)
.fail(self.errorSave)
Is it possible to not use the full path?
I wonder whether, there is an error in ASP.NET MVC configuration.
On developer environment it works with simplified URL in ajax call.
And I can't believe that Microsoft forces developers to adjust URL address on every productive system.
I bet the Url.Action construct will work. It ties into your routing configuration to produce a valid url.
In .JS Script
url:'#Url.Action("api","cartitems")',
In .xxhtml
url:'#Model.YourPostabckUrlVariable',
I have implemented one MVC application with Basic implementation as per this
Now when I call this action method from my Webform application using AJAX, it did not asking for credentials and throwing an 401 error.
This is my ajax function:
function testSS()
{
$.ajax({
url: 'http://localhost:52099/Controller/Sample',
type: 'GET',
success: function (result) {
$("#ctl00_cphPageContent_SSlbl12").val(result);
},
error: function (xhr) {
alert(xhr);
}
});
}
How can I call authenticated method from webform project?
Thanks!
In order to make this work, you need to add an Authorization header, containing base64-encoded login and password.
To encode base 64, you can use btoa()
var authHeader = window.btoa("login:password");
Now, you can add this header (see $.ajax documentation)
$.ajax({
url: 'http://localhost:52099/Controller/Sample',
headers: { Authorization: authHeader },
type: 'GET',
success: function (result) {
$("#ctl00_cphPageContent_SSlbl12").val(result);
},
error: function (xhr) {
alert(xhr);
}
});
Please note this will not be very secure, because login/password are known from the client, and sent in an easily readable way, so very easy to be found by an attacker.
If the webform page and MVC action are in the same project, and the user is ogged on your website, you should probably use the asp.net [Authorize] attribute instead : it will check the auth cookie, and not rely on a login/password, it's much more secure.
I have a little problem with IIS. I am developing C# MVC application which uses jqgrid. One of my grids with id #academies_griddata is populated with data specific to chosen academy (Id is then determined). At the beginning no academy is chosen so i set the parameters of jqgrid as follows:
`...$("#contracts_griddata").jqGrid({
mtype: 'GET',
datatype: 'local',
colNames: [...`
After academy is chosen in jquery code I set paramters as follows:
var Id = $("#Id").val();
$("#contracts_griddata").jqGrid('setGridParam',
{ url: "/Academies/ContractGridData?AcademyId=" + Id, datatype: "json" });
$("#contracts_griddata").trigger('reloadGrid');
When I run the code in Visual Studio it works like a dream, but after publishing to IIS I get error:
404 Not Found
When I check a request in Firebug it is exactly the same e.g.:
http://localhost/Academies/ContractGridData?AcademyId=551&_search=false&nd=1383686029082&rows=5&page=1&sidx=&sord=asc
Anyone can help please?
Recently I was faced with some strange server behavior - it started to return a 500 error at several ajax POST requests. All was working fine before. GET requests work fine. I have made Visual Studio trace my code exceptions but I still cannot see requests coming in while debugging. Do you have any ideas?
Example
$.ajax({
type: "POST",
url: "/home/some-action",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
}
});
[POST("some-action")]
public ActionResult SomeAction()
{
return new JsonNetResult("success request");
}
Update
I use this prefix for my controller:
[RoutePrefix("home")]
So you're using Attribute Routing?
I can't see your Controller but I assume you're missing the Route for /home in the controller, or removed it from [POST("/home/some-action")]
We need more information though. Current assumption is the URL doesn't exist.
I tried all my efforts, but just can't understand where the error lies. I searched the google also but did not find any good solution. It is not actually calling the controller/action in mvc. The same is running good in the other parts of the project.
I have a contrller "RB" under a folder "MVC", the action is defined as "SS".
and I am firing following code from my javascript file :
var sSch = function (request, response) {
var t = request.RF.substring(0, 1);
var d = new Date(request.RNR);
$.ajax({
url: "/MVC/RB/SS",
type: "POST",
dataType: "json",
data: {
_rId: request.ReportId,
_date: d.toString(),
_fcy: t
},
success: function (data) {
alert('Success');
},
error: function (data) {
alert('Error');
}
});
};
I am calling this function onClick of a button and properly getting the values in Request variable, but it is not anyhow calling the Controller/Action there.
On firebug I tested it throws the exception "ReferenceError: url is not defined". I am using MVC3 under VS 2010.
Please Help.
You have to define your action properly instead of
url: "/MVC/RB/SS",
use
url: #Url.Action("SS", "RB")
For the url you have 'MVC/RB/SS' this is relative to your current directory a quick test would be to put in url: "../MVC/RB/SS" or url: "../../MVC/RB/SS" depending on how deep you page is in the site structure.
Another way would be to try this:
url: "#Action("/MVC/RB/SS")",
this will create the correct url at the correct level for you and should be picked up.
Try this:
url: "~/MVC/RB/SS",
This will resolve to path "http://site-name/MVC/RB/SS".
What does Firebug say?