All I want to do is post a zipcode to a controller, do some stuff to the zipcode, and post back the changes to the zipcode. But my parameter a is always null. Can anyone tell me what I am doing wrong?
Here is my code:
View:
<input type="text" id="zipcode" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var serviceURL = '/Employer/employer/index';
var zipcode = $("#zipcode").val();
$("#zipcode").blur(function () {
$.ajax({
type: "POST",
url: serviceURL,
data: {'a':zipcode},
contentType: "application/json; charset=utf-8",
success: successFunc,
error: errorFunc
});
function successFunc(data, status) {
alert(data);
}
function errorFunc() {
alert('error');
}
});
});
Controller:
public ViewResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string a)
{
return Json("test", JsonRequestBehavior.AllowGet);
}
index.phpor something, add the extension.$("#zipcode").val()is null. Can you add your HTML code?