我正在尝试通过 jQuery 和 ajax 将来自 textBoxes 的输入值的数据发布到 restful 服务。但我做不到。
这是我的 HTML 代码:
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="LoginHtml.js">
</script>
</head>
<body>
<div id="registration">
<h2>Login Account</h2>
<form id="registerUserForm">
<fieldset>
<p>
<input id="txtUserName" type="text" required="required" autofocus="true" placeholder="User Name" />
</p>
<p>
<input id="txtPassword" type="password" required="required" placeholder="Password" />
</p>
<p>
<input type="button" id="submitForm" autofocus="true" /><br>
<input type="button" value="Login" onclick="Call()" ></input>
</p>
</fieldset>
</form>
</div>
</body>
</html>
这是我的javascript代码:
function Call() {
jQuery.support.cors = true;
var data = {};
data.uid = document.getElementById('txtUserName');
data.pwd = document.getElementById('txtPassword');
$.ajax({
data: jQuery.toJSON(data),
dataType: "json",
url: "http://:xxxxxx:8080/Service1/Login",
type: "GET",
contentType: "application/json; charset=utf-8",
success: function (result) {alert("success");
alert(result.d);}
error: function OnError(request, result, error) {
alert(result);
}
});
这是我的服务代码:
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public UserProfile Login(string uid, string pwd)
{
UserProfile oUser = null;
//UserDao userDao;
using (UserProfileDataContext db = new UserProfileDataContext())
{
var u = db.Users.FirstOrDefault(o => o.Username == uid && o.Password == pwd);
if (u != null)
{
oUser = new UserProfile();
oUser.Id = u.Id;
oUser.Username = u.Username;
oUser.Password = u.Password;
oUser.Email = u.Email;
}
}
return oUser;
}
告诉我哪里错了。