1

I'm trying to send an array to an action from a controller in Yii but I'm getting 404 all the time. This is how I'm sending the data from javascript:

$.ajax({
    url: baseUrl + '/user/validateUser',
    data: $('#usr').val(),
    type: 'post',
    dataType: 'json',
    success: function(res)
    {
        console.log(res);
    }
});

In my UserController i have the following action:

public function actionValidateUser() {
    if ($_POST > 0){
        $username = $_POST['username'];
        var_dump($username);
    }
}

I'm getting this response:

Request URL:http://localhost/myapp/user/validateUser
Request Method:POST
Status Code:404 Not Found

What am I doing wrong? In Zend I did this and it worked just fine. Here ... I don't know.

7
  • Please, post your config with UrlManager settings. You get 404 when You open http://localhost/myapp/user/validateUser? Also You can try to use relative path /user/validateUser instead baseUrl + '/user/validateUser' Commented Aug 11, 2014 at 20:07
  • @jonijones I realized that none of the urls are working, on my local machine. Commented Aug 11, 2014 at 20:20
  • 'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'rules'=>array( '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), ), Commented Aug 11, 2014 at 20:20
  • Do you have your rewrite rules correctly set up in your .htaccess? What do you get if you open http://localhost/myapp/index.php?r=user/validateUser in the browser? If you get a 200 OK response there is something wrong with your .htaccess rewrite. BTW you should not do a if ($_POST > 0). Better would be if (isset($_POST) && count($_POST)) if you want to check for an existing POST array. Commented Aug 11, 2014 at 20:50
  • @chris--- hahaha, it worked how you said. but i don't know how to rewrite it. That's a problem, too. Commented Aug 11, 2014 at 20:54

2 Answers 2

1

Seems like your apache rewrite rules are not correct/present. Create/edit the file named .htaccess under your htdocs folder. Put the following in it:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

Save it and restart apache. Make sure you have mod_rewrite enabled in your apaches httpd.conf.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry, but still not working. I edited the local .htaccess file, from myapp but still not working. It looks like yours. p.s.: I'm using Debian with Apache, not xampp.
Weird. Check your apache config that mod_rewrite is enabled under your modules section in the apaches httpd.conf.
0

your ajax call receive a string as data, and there is not information about which field this string represent. You should indicate which field this data represents.

Try the code below:

$.ajax({
    url: baseUrl + '/user/validateUser',
    data: { username: $('#usr').val() },
    type: 'post',
    dataType: 'json',
    success: function(res)
    {
        console.log(res);
    }
});

your original request is generating some call to //localhost/myapp/user/validateUser?<username>. but the code above will generating a call to //localhost/myapp/user/validateUser?username=<username>

1 Comment

still not working. i guess it's a config error. none of my actions are working on my local machine, on the servers are all good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.