Reputation: 691
My Ajax Code is
function UserCheck() {
var q = document.getElementById("username").value;
$.ajax({
url: '@Url.Action("Checks","Ajaxx")',
data: {
'userdata': q
},
type: "POST",
dataType: "html",
success: function (data) {
//------------
alert("insuccess");
document.getElementById("username").innerHTML = data.toString();
}
});
}
Am using this ajax code to check whether the entered username is exists My controller name is Ajaxx and method is checks
Method is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CinemaApplication.Models;
namespace CinemaApplication.Controllers
{
public class AjaxxController : Controller
{
//
// GET: /Ajaxx/
[HttpPost]
public string Checks(string userdata)
{
string tmp = "success";
using (OurDbContext db = new OurDbContext())
{
var SeachData = db.Logins.Where(x => x.username == userdata).FirstOrDefault();
if (SeachData != null)
{
tmp = "Fail";
}
}
return tmp;
}
}
}
This is the whole code of my controller. It is dedicated to this ajax . the value of username field is correctly arrived in q ,but i don't know that the method (Checks) in controller(Ajaaxx) is working
Upvotes: 0
Views: 45
Reputation: 691
Ajax code
function Checks()
{
var unm = document.getElementById("username").value;
$.ajax({
type: "POST",
url: "/Ajaxx/Checks",
data: '{user:"' + unm + '"}',
contentType: "application/json; charset=utf-8",
datatype: "json",
success: function(result)
{
var mess = $("#Status");
if (result)
{
mess.html('Username available');
mess.css("color", "green");
}
else{
mess.html('Username not available');
mess.css("color", "red");
}
}
});
}
</script>
var mess is holding the id of the span Controller
[HttpPost]
public JsonResult Checks(string user)
{
bool tmp = true;
using (OurDbContext db = new OurDbContext())
{
var SeachData = db.Logins.Where(x => x.username == user).FirstOrDefault();
if (SeachData != null)
{
tmp = false;
}
}
return Json(tmp);
}
Upvotes: 0
Reputation: 1546
Try using the following properties in your jquery call:
dataType: 'json',
contentType: "application/json; charset=utf-8"
Upvotes: 1