-1

I am trying to use AJAX to allow a user to input an ID number in order to populate another area on my page. I want to use AJAX to accomplish this. I haven't used AJAX that much, especially while using MVC. My AJAX should execute when my checkIdBtn is clicked but every time I click it, it says "The resource cannot be found. Requested URL: /Events/Search". I can't figure out what is wrong with my URL (im guessing it can't find my controller/action method?). My controller is my HomeController and the action method is getEmpInfo. Below is the code for my AJAX:

<script type="text/javascript">
$(document).ready(function () {
    $('#CheckIdBtn').click(function () {
        var empId = $('#empId').val();
        $.ajax({
            url: '/Home/getEmpInfo',
            contentType: 'application/html; charset=utf-8',
            data: { id: empId },
            type: 'GET',
            dataType: 'html'
        })
        .success(function (result) {
            $('#EmpInfoDiv').html(result);
        })
        .error(function (xhr, status) {
            alert(status);
        })
    });
});
</script>
6
  • what is the result when you type http://yourserver/Home/getEmpInfo in browser? Commented Sep 23, 2014 at 12:53
  • it finds it... tells me I have a null value for my parameter for the controller. Commented Sep 23, 2014 at 12:55
  • Is the button's click event firing? (ie does the page reload) if so you need to add 'preventDefault()` Commented Sep 23, 2014 at 12:57
  • The resource cannot be found. Requested URL: /Events/Search: somehow you are calling http://yourserver/Events/Search Commented Sep 23, 2014 at 12:58
  • I know..In my address bar is puts /Events/Search instead of /home/getEmpInfo but i am not calling events/search anywhere. Commented Sep 23, 2014 at 13:00

3 Answers 3

2

Few points -

  1. You are posting some data to the server, so, use type: 'POST' instead of 'GET'.
  2. Remove dataType and contentType.
  3. Make sure the action named "getEmpInfo" receives a parameter named id.
Sign up to request clarification or add additional context in comments.

1 Comment

I updated it according to your points but still the same results.
0

I suspect you're posting to your page again when you click. Try adding e.preventDefault(); to your click handler (I also fixed your ajax call anomalies):

$('#CheckIdBtn').click(function (e) {
    e.preventDefault();
    var empId = $('#empId').val();
    $.ajax({
        url: '/Home/getEmpInfo',
        data: { id: empId },
        type: 'POST'
    })
    .success(function (result) {
        $('#EmpInfoDiv').html(result);
    })
    .error(function (xhr, status) {
        alert(status);
    })
});

Comments

0

When I have this error I run project from index file then this error is not appear. You can try open Index or html file then click on run button. I think your error is ... You are mixing views and partial views make sure your views and partial views are not mixed up.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.