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',
Related
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
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.
Recently I learned that you can take a script manager and set the EnablePageMethods="true" this is something I would like to do but is there any security implications ?
Also, Is there a newer better way to this ?
All that EnablePageMethods="true" really does is generate an inline JavaScript proxy that allows for calling back to ASP.NET AJAX Page Methods via the PageMethods syntax.
I prefer using jQuery via its .ajax() method to call ASP.NET AJAX Page Methods, like this:
Code-behind
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
JavaScript:
$.ajax({
type: "POST",
url: "PageName.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
Note: Since ASP.NET AJAX Page Methods automatically serialize their response to JSON, then "json" is the return dataType of the AJAX call. contentType describes what you are sending to the server.
It does not have any more security implications than would adding any ordinary web page or service to a website. The needed precautions are the same -- don't use any inputs without validating them first, don't inadvertently return sensitive information in error messages, etc.
There is a newer way, and arguably better, if you don't mind switching to a new platform: ASP.Net MVC, and the related "Web API". (You can use Web API with Webforms also, thanks to Karl Anderson for noting in the comments.)
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?