我想通過jQuery和ajax將數據從textBoxes輸入到restful服務。但我無法做到。將數據從HTML中的textBox發佈到使用jQuery的Restful服務AJAX
這裏是我的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;
}
告訴我在哪裏,我錯了。
我試過了但不工作:/ –