0

just trying to learn ajax on asp.net mvc,but dont know whats wrong with it?

my controller class method=>

 [HttpPost] public JsonResult Getdata(string batchcode) { ///var studentid = db.Studentassignbatches.Where(x => x.batch_code == batchcode).Select(x => new Studentassignbatche { UserId = x.UserId }).ToList(); return Json("hi",JsonRequestBehavior.DenyGet); } 

and my view and ajax request>

<select onchange="ShowBatchCode()" id="BatchList" class="form-control input-lg"> <option class="pull-left" value="CCNA Security-1">CCNA Security-1</option> <option class="pull-left" value="CCNA Security-2">CCNA Security-2</option> <option class="pull-left" value="JNCSP-SEC-1">JNCSP-SEC-1</option> <option class="pull-left" value="Oracle Database 12c-1">Oracle Database 12c-1</option> </select> 

and ajax=>

<script type="text/javascript"> function ShowBatchCode() { var batchcode = $('#BatchList').val(); $.ajax({ type: 'POST', dataType: 'json', contentType: 'application/json', url: 'Getdata', data: { batchcode: batchcode }, success: function (data) { alert(data); }, error: function (result) { alert('Something Went Wrong!'); } }); } 

and it always show me a alert "Something Went Wrong!".so i debug my web page and i found=> enter image description here

505 Internal server Error.i dont know how to solve it.and try to remove [Httppost] and also try use

 data: batchcode, 

but nothing works for me.help me please?anybody!!!!!

3
  • contentType: 'application/json', , you need change data to json Commented May 7, 2017 at 14:37
  • data: JSON.stringify({ batchcode: batchcode }) Commented May 7, 2017 at 14:39
  • Try giving controller name if name is YourController then then url should be 'Your/Getdata' Commented May 7, 2017 at 14:57

1 Answer 1

2

You have indicated:

contentType: 'application/json', 

but you sent application/x-www-form-urlencoded here:

data: { batchcode: batchcode }, 

So either get rid of the contentType: 'application/json' line or make sure that you are sending JSON to the server:

data: JSON.stringify({ batchcode: batchcode }), 
Sign up to request clarification or add additional context in comments.

6 Comments

Not Working For ME!
What is the exact response body from the server? You can see it from the developer toolbar in your browser.
That's not the response body sent from the server. Could you please show the exact HTML returned by the server as a response to the AJAX call? Just click on the AJAX call in your developer toolbar and look at the response tab.
hope this will help you?
OK, you are getting 404 Not Found. So it looks like your url parameter is wrong. Try specifying the correct url to your controller action.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.