I checked the JSON response on my C # web method, so I don't think the problem is.
I am trying to parse the result using simple jQuery $ .ajax, but for some reason I cannot get the method to run and parse the result correctly, by the way, it seems it cannot get the function to run the result. Are there any restrictions on the size of the JSON object that can be returned.
I also removed this code from the Site.Master page because it always updated when I clicked a simple button. Do tags work correctly with jQuery elements, such as entering a button that I grab from the DOM?
function ajax() { //var myData = { qtype: "ProductName", query: "xbox" }; var myData = { "request": { qtype: "ProductName", query: "xbox"} }; $.ajax({ type: "POST", url: "/webservice/WebService.asmx/updateProductsList", data: {InputData:$.toJSON(myData)}, contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { // var msg = {__type: "Testportal.outputData", id: "li1234", message: "it work!", myInt:101} alert("message=" + msg.d.ProductName + ", id=" + msg.d.Brand); }, error: function (res, status) { if (status === "error") { // errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace var errorMessage = $.parseJSON(res.responseText); alert(errorMessage.Message); } } });
}
And the page:
<asp:Button ID="Button1" runat="server" OnClientClick="ajax();" Text="Button" />
And the Serverside web method:
public class WebService : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public OutputData updateProductsList(InputData request) { OutputData result = new OutputData(); var db = new App_Data.eCostDataContext(); var q = from c in db.eCosts select c; if (!string.IsNullOrEmpty(request.qtype) && !string.IsNullOrEmpty(request.query)) { q = q.Like(request.qtype, request.query); }
And helper classes:
public class OutputData { public string id { get; set; } public List<App_Data.eCost> products { get; set; } } public class InputData { public string qtype { get; set; } public string query { get; set; } }
json jquery ajax
jordan.baucke
source share