9

When I pass POST data through AJAX to my controller it is empty. In the AJAX the data is still there but after I send it the controller it says it is empty.

AJAX:

  function usernameCheck()
{
    var input = document.getElementById("usernameInput");
    var icon = document.getElementById("userIcon");
    var xmlhttp,
        username = document.getElementById("usernameInput"),
        message = document.getElementById("usernameMessage");

    if (username.value != "") {
        if (window.XMLHttpRequest) {
           // code for IE7+, Firefox, Chrome, Opera, Safari
           xmlhttp=new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {

                // FOR DEBUGGING
                console.log(xmlhttp.responseText);

            }
        }
    }

    xmlhttp.open("POST", "usernamevalidation", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("username=" + username.value);
    }
  }

Routes.php:

Route::post('usernamevalidation', 'UserController@validateUsername');

UserController.php:

class UserController extends BaseController {

    public function validateUsername() {

       // FOR DEBUGGING
       dd(Input::all());

    }
}

The code that I console.logged (which is empty and should contain the username):

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=0)</i>
  <i><font color='#888a85'>empty</font></i>
</pre>
7
  • 1
    Try dd(\Request::getContent)) and see what that gives you. Commented Feb 18, 2015 at 13:11
  • debug the ajax request first. Open your browser console and check the ajax request Form Data in Header. Commented Feb 18, 2015 at 13:18
  • 1
    it gives me a 500 internal server error, I tried it with plain php $_REQUEST, and that is empty too. Commented Feb 18, 2015 at 13:19
  • I already did in AJAX, it works there, it says username = (whatever I entered) in the POST data. The problem is that at the moment it goes in the controller it's empty Commented Feb 18, 2015 at 13:21
  • * It does work with the Request::getContent() Commented Feb 18, 2015 at 13:22

1 Answer 1

7

In the awareness that I'm giving my answer here one year later since the last comment, I've stumbled on the some error using Laravel 5.2 and working with XMLHttpRequest objects: finally I ended to analyze and compare post request headers, which led me to simply setting both:

xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8');

That solved my empty responseText. Hope this can help someone else or can be used for future reference.

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

1 Comment

@Pila, observing the OP's code listing, you should call the setRequestHeader on the XMLHttpRequest object after the call to its open method.

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.