Codeigniter ajax gives 404 not found - ajax

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'); ?>",

Related

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');

Ajax Routing Calling Controller Name Twice

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")'

Calling controller method (ASP MVC3) method from ajax doesn't work

I am using the following syntax to make a call to controller method from ASP page.
$.ajax({
url: 'ControllerName/MethodName',
type: 'POST',
contentType: 'application/json;',
data: JSON.stringify({ param: param1}),
success: function () {
alert("Success!!!");
},
error: function () {
alert("Failed!!!");
}
});
I have two ASP pages (views), both having same controller. If I call above method from first page, controller method gets called successfully. But if call same method from second page I get alert message "Failed". Also I tried using GET type, tried with other controller methods and all. Nothing will be called from second view. can anyone help me what can be problem? I am new to MVC.
Since your ajax is expecting result of JSON data from your Controller method do you have return Json(data, JsonRequestBehavior.AllowGet)?
Try change content type to:
contentType: 'application/json; charset=utf-8'
or/and specify url using mvc helper like:
url: #Url.Action("action"),
Works in my example. Hope it will help.

WORDPRESS : Ajax and template

I have a question.
How can I use Ajax on my templates...
in single.php I have :
$.ajax({
type: "POST",
url: "http://www._____wp-content/themes/MS-MangoBerry___/myajax.php",
data: "yo",
cache: false,
success: function(data)
{
alert("yes");
}
});
And in myajax.php, I have
$(document).ready(function() {
alert("ok"); });
Then I have an error : Fatal error: Call to undefined function get_header() in myajax.php
Why ?
Thanks in advance.
Please also have a look at this article http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/#js-global
It suggests that all AJAX requests should be submitted to /wp-admin/admin-ajax.php
And the you could hook the request by using this code in functions.php
add_action('wp_ajax_your_ajax_action_name', 'method_name');
add_action('wp_ajax_nopriv_your_ajax_action_name', 'method_name');
Then you could implement a method in functions.php
function method_name()
{
// do something or echo XML to return stuff
}
On the request you also need to send a parameter name 'action' with value of the action name.
in this case it would be action=your_ajax_action_name.
Hope this help :)
wordpress has a built ajax url that you need to use. this post will help you out.
http://geekpreneur.blogspot.com/2009/06/how-to-use-wpajax-in-wordpress.html
the tricky thing is how wordpress knows which function will accept your call back. it happens by adding an action. the hook of the action is your ajax action prepended with wp_ajax_

Resources