I have a jquery code which opens a dialog. This dialog is a partial view which renders from the response of a jquery Ajax call "http://test.com/ControllerA/ViewDetails". Ajax call code looks like below
$.ajax({
url: "ViewDetails",
type: "GET",
dataType: "html",
The dialog box has button which has to make another Jquery Ajax call (this has go against a different controller and action). Ajax code looks like below.
$.ajax({
url: "ControllerB/Search",
type: "GET",
dataType: "html",
The above ajax call fails to find search action because The URL will get changed to http://test.com/ContollerA/ControllerB/Search.
I feel this is something related to a route config. But i need some directions from you all.
$.ajax({
url: "#Url.Action("ViewDetails", "ControllerA")",
type: "GET",
dataType: "html",
and
$.ajax({
url: "#Url.Action("Search", "ControllerB")",
type: "GET",
dataType: "html",
This way you're using the route table and not generating urls willy nilly
The best way is to use
url: '../ControllerA/ViewDetails'
it worked for me when the culture is in url
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',
From Asp.Net MVC UI(View) I am calling a Asp.Net WebApi through Ajax Call.
$.ajax({
type: "POST",
contentType: "application/json",
url: "api/UpdateEmployees",
data: undefined,
success:
function () {
EmployeesPage();
},
error: function (err) {
Error(err);
}
});
When I publish the WebSite in IIS as a Site it is working fine.But if I publish the WebSite under "Default Web Site" it is not working because it is expecting the url as "EmployeeWebSite/api/UpdateEmployees" where EmployeeWebSite is my virtual directory under "Default Web Site".
I should append url based on where the WebSite is hosted.Can anyone help me with this? I want my website to work in both cases.
I am deploying both ways using localhost.
use this code in layout.chtml or In your current View
<script type="text/javascript">
var baseurl = '#VirtualPathUtility.ToAbsolute("~/")';
</script>
After that u can use your Base URL varible
$.ajax({
type: "POST",
contentType: "application/json",
url: baseurl +'api/UpdateEmployees',
dataType: "json",
success:
function () {
EmployeesPage();
},
error: function (err) {
Error(err);
}
});
Tip : Dont put undefined in your $.ajax if you not parsing any data Remove it .
Have a grate day
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.
Is it possible to load and save data to/from a grid using c# code in the back from an aspx page, or does one have to use a web service (or PHP)? I have tried and failed using JSON.Net to map a very simple structure to code a backend structure
Is it possible to use JQuery (an ajax GET I presume) to make a call to a method in the backend code file (.aspx.cs)? I have tried using code from various posts on this forum, but there is little information on the backend code (c#), and all seem to refer to web services. Any help/advice would be much appreciated.
Here is the JavaScript code associated:
var handsontable = $container.data('handsontable');
$(document).find('button[name=load]').click(function () {
$.ajax({
url: "Default.aspx/getJSData",
dataType: 'json',
type: 'GET',
//contentType: "application/json; charset=utf-8",
success: function (res) {
handsontable.loadData(res.data);
$console.text('Data loaded');
},
error: function () {
$console.text('Load error');
}
});
});
You still need to do an Ajax call but you don't need to do a web service (you can). The function you want exposed to the Ajax call put the [WebMethod] Attribute on and an use a Script Manager with the EnablePageMethods attribute set to true.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
Method to Access:
[WebMethod]
public static void SomeFunction(string message, string name)
{
}
Ajax call using jQuery
(function($) {
$.ajax({
type: "POST",
url: "test.aspx/SomeFunction",
data: "{message:'Hello World', name: 'Bob'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert('success');
}
});
});
Reference: Using the WebMethod Attribute
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?