I am using fluent NHibernate with WebForms and I am trying to build a page where I allow a user to post a status update on their profile page and I a using a .asmx WebService to post the data to the database and then returnig a StatusUpdate instance to the page to be used by jQuery. I have a couple problems.
1) First off when I return a string from the WebService(i was testing) the textbox where the user enters their status doesnt empty the contents. And since the page doesnt refresh even if I manally clear out the textbox and put in something else, it still posts the previous status to the database again. How do I fix this?
2) Secondly, when I return the StatusUpdate Object from the Webservice I cant the results to display anything. Like I sad, Im using jQuery to make an AJX call to the WebService.
Here is my code:
User Profile Page Javascript:
var status1 = $("#statusBox").val();
var userID = $("#MainContent_userID").val();
function SetStatus() {
$.ajax({
type: "POST",
url: "http://localhost/Sports/Services/UserWebService.asmx/SetStatus",
data: '{"status": "' + status1 + '", "userID": "' + userID + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnError
});
}
function OnSuccess(response) {
$("#statusBox").empty();
$("#MainContent_status").html(response.Status).fadeIn(1000);
}
function OnError(request, status, error) {
alert(request.statusText);
}
WebService:
[WebService(Namespace = "Sports.Services")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class UserWebService : System.Web.Services.WebService
{
private IUserSession _userSession;
public ISession Session1
{
get { return NHibernateSessionModule.GetCurrentSession(); }
}
[WebMethod]
public StatusUpdate SetStatus(string status, Guid userID)
{
_userSession = ObjectFactory.GetInstance<IUserSession>();
StatusUpdate update = new StatusUpdate();
update.StatusDateTime = DateTime.Now;
update.StatusLikes = 0;
update.UserID = userID;
update.Status = status;
Session1.SaveOrUpdate(update);
return update;
}
}
Any help would be greatly appreciated... Ive looked at probably 100 pages trying to find an answer and none have worked..
1 Answer 1
For #1, you need to move the .val() calls inside the method so they're fetched at the correct time, like this:
function SetStatus() {
var status1 = $("#statusBox").val();
var userID = $("#MainContent_userID").val();
$.ajax({
type: "POST",
url: "http://localhost/Sports/Services/UserWebService.asmx/SetStatus",
data: '{"status": "' + status1 + '", "userID": "' + userID + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnError
});
}
Though you should consider making this as an object and using .stringify to handle any special characters.
For #2, asmx web services like to wrap the object, so it doesn't look like this:
{ "Status": "My Status", "UserID": 12, "StatusLikes": 0..... }
It actually looks like this:
{ "d": { "Status": "My Status", "UserID": 12, "StatusLikes": 0..... } }
So instead of this:
$("#MainContent_status").html(response.Status).fadeIn(1000);
You need this:
$("#MainContent_status").html(response.d.Status).fadeIn(1000);