开发者

How to pass parameter back from asp.net web service if an insert to a database was successful? ASP.NET/jQuery/AJAX/JSON

I have a web form and I use jQuery/AJAX/JSON to send objects to a we开发者_开发技巧b service using:

$.ajax({
type: "POST",
url: "SynchroniseCustomers.asmx/synchroniseCustomers",
data: JSON.stringify(customerObj),
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (xhr, status) {},
success: function (msg) {}
});

From the web service I want to check if the insert into the database was successfull, return a variable with an ID and pass this ID to a function. In the object I have the ID so I could have:

success: function (msg) {deleteCustomer(ID);}

But this only checks if the data was passed to my method in the web service? I have followed this example

http://encosia.com/2009/04/07/using-complex-types-to-make-calling-services-less-complex/

and in the class Person I get a message back from the database server telling me if the insert was successfull or not so like:

if (successfull)
{
    return ID;
}

Is there a way to get this ID back to the web form and use this in a variable?

Thanks in advance.


You want to return a result object with an isSuccess and id property. You can then do the following:

success: function (result) {
  if (!result.isSuccess) {
    // display friendly error message indicating that the db insert failed
  } else {
    var id = result.id;
    // do client side processing with primary key returned from db insert
  }
}

How you return the result object depends on your web services framework (e.g., WCF, MVC, WebMethod, etc.). The framework will serialize your .NET object to a JSON encoded result (e.g., { isSuccess: true, id: '1234'}).

As an example, in ASP.NET MVC you simply return a JsonResult using Json.(MySerializableResult). If you post what you are using for web services, I'm sure you'll get a specific answer to that framework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜