Ajax Routing Calling Controller Name Twice - ajax

I am trying to do a simple call to my controller via ajax. The RouteConfig has not been changed and set to the default values. When I make ajax call, the Url that is requested in the "Network" debugging tools is:
'http://localhost:59275/Leaderboard/Leaderboard/GetPosition'
This is causing a 404 because the Controller, Leaderboard, is being added into the Url twice. The correct url should be
'http://localhost:59275/Leaderboard/GetPosition'
My ajax call is as follows:
$.ajax({
url: 'Leaderboard/GetPosition',
type: "GET",
dataType: 'xml',
success: function (data) {
$('#results').html(data);;
}
});
and my controller is as follows:
public class LeaderboardController : Controller
{
[Webmethod]
public static DataTable GetPosition()
{
// do stuff
}
}

If the root URL of the page that request the ajax is "Leaderboard" then the url on the ajax call should only "GetPosition"
Or you can use "/Leaderboard/GetPosition" with "/" in front

Use Url.Action helper for generating correct url.
Change this:
url: 'Leaderboard/GetPosition'
to this:
url: '#Url.Action("GetPosition","Leaderboard")'

Related

Method not allowed while using ajax put request asp.net mvc

I have the following AJAX request:
$.ajax({
url: "/api/newrentals",
dataType: "json",
type: "PUT",
data: columnDate
}).done(function() {
toastr.success("Rentals succesfully recorded.");
}).fail(function() {
toastr.error("Something unexpected happened.");
});
And here's my CONTROLLER:
using System.Web.Http; //I used the correct reference
public class NewRentalsController : ApiController {
[HttpPut]
public void updateRentalReturnDate(string columnDate){
var date = columnDate;
}
}
When I hit some button, the ajax request is executed but it does not reach the controller. I get the following error :
"message": "The requested resource does not support http method 'PUT'."
I have read many answers but nothing helped me. For instance, I made sure that the paramater name in ajax and in the controller should be same. I also put the [HttpPut] annotation in the controller method. What exactly am I missing here?

How to pass controller action in Jquery AJAX Call in Oxid eSHOP

I am working on Jquery AJAX in OXID eSHOP.
I want to pass proper action (function) name in AJAX url parameter.
Below is my code:
jQuery.ajax({
type: "POST",
data: {
variable: value
},
url: "",
success: function(data) {
}
});
I want to call one function in url parameter.
But don't know how to pass its value in url paramaeter.
Anyone knows then please help me.
You should include function name as URL parameter, just like the shop does it in most situations:
http://your-shop.com/index.php?cl=controller&fnc=functionname

Routing to pass parameters Laravel 5.3 and AJAX

I have to pass parameters per post with ajax but not working on the console I get this:
POST http://localhost:8000/prueba2 405 (Method Not Allowed)
This is my routing:
Route::get('prueba2', 'HomeController#index');
This is my ajax:
$.ajax({
url: '{{url('prueba2')}}',
type: 'POST', // Send post data
data: 'type=fetch',
async: false,
success: function(s){
json_events = s;
}
});
This is my controller:
public function index(){
return 'hola';
}
All this is a test and is not the final driver nor the final ajax, but it seems to be some response by the controller. But unfortunately I get a 405.
If someone can help me with this serious problem it would be a lot of help
You are receiving a MethodNotAllowedException because you defined a GET route with Route::get('prueba2', 'HomeController#index');, but you do a POST request.
Change your AJAX type to GET or use Route::post().
The last one would look like:
Route::post('prueba2', 'HomeController#index');

Codeigniter ajax gives 404 not found

I'm trying to use ajax with Codeigniter, but I'm getting a 404 (not found) error.
For ajax I do this:
$.ajax({
type: "POST",
url: "/index.php/ajax/user-sign-up-via-email",
data: {
email: email,
password: password
}
});
For my Routes.php I have this:
$route['ajax/user-sign-up-via-email'] = "UserSignUpViaEmailAjaxController";
Am I missing something specific to Codeigniter?
Is it related to first URL parameter being the controller, second parameter being the function to call within the controller?
As per comments:
Make sure that the controller you are calling (UserSignUpViaEmailAjaxController) has an index() function. Since you are remapping the uri directly to a controller without specifying a function it will default to the index() function and will 404 if it can't find one.
i do this normally .. u can try this also
url: "<?php echo site_url('customersController/addCustomer'); ?>",

Unexpected GET request after POST on AJAX call to MVC Action

I have an action method that is invoked with POST method over AJAX. Method looks like this:
[HttpPost]
public ActionResult Save(MyModel model)
{
/// do something and save
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
What happens is that after this method is call by the $.ajax, from unknown reason follow up GET request is made to same controller and same action. This call is not made anywhere and it's probably implemented either in browser or in ASP.NET MVC as Post/Redirect/Get pattern which I don't need in this case.
Is there a way to disable this programmatically because GET counterpart of Save method is not implemented and server returns 404 with HTML contents in it.
EDIT, this is the client-side AJAX script:
function saveDonkey(donkey) {
var jsonData = JSON.stringify(donkey);
$.ajax({
url: "/Donkey/Save",
data: jsonData,
contentType: "application/json",
dataType: "json",
type: "POST",
success: function () {
fetchPage(); /// This just issues GET request to another predefined method
}
});
}

Resources