如何从JQuery获取WebMethod的返回值

时间:2022-12-05 17:07:05

I am tryring to get the return value of WebMethod from JQuery call, but I am getting "undefined" message. Here is my code below

我试图从JQuery调用获取WebMethod的返回值,但我收到“未定义”消息。这是我的代码如下

$.ajax({
        type: "POST",
        url: "Receipt/BarcodeEntered",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "text",
        success: function (msg) {
                    alert(msg.d); // This displays "Undefined"
                    alert(msg);   // This displays the whole html
                 }
});

and the WebMethod is below

WebMethod如下

[WebMethod]
public static string BarcodeEntered() 
{
    return "test_string";
}

how can I get the value from WebMethod and display it on client side?

如何从WebMethod获取值并在客户端显示?

3 个解决方案

#1


2  

WebMethod officialy only can return XML or JSON. Default is json, so whatever you return gets converted to json

WebMethod官方只能返回XML或JSON。默认是json,所以无论你返回什么都转换为json

change in JQuery dataType: "json",

更改JQuery dataType:“json”,

$.ajax({
        type: "POST",
        url: "Receipt/BarcodeEntered",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
                    alert(msg);
                 }
});

and you should return class not single string. because string can not be converted valid json object.

你应该返回class而不是单个字符串。因为字符串无法转换为有效的json对象。

public class SampleClass{
    public string Message {set; get;}
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public SampleClass BarcodeEntered()
{

        return new SampleClass(){
        Message  = "Sample message"
    };

}

#2


0  

You need to return JSON, let me write down an example.

你需要返回JSON,让我写下一个例子。

public JsonResult DoStuff()
{
    string text = "text";

    return Json(text, JsonRequestBehavior.AllowGet);
}

#3


0  

Here is the working demo I am using this in my asp.net page. The data is will be in d property.

这是我在asp.net页面中使用的工作演示。数据将在d属性中。

JQuery code.

    $.ajax({
    type: "POST",
    url: "/Subfolder/MyPageName.aspx/myWebMethodName",
    data: '{}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
      if (msg.d == "OK") {
       alert("OK")
      } else {
         alert(msg.d);
      }
    }
   });

C# Code

[WebMethod]
public static string myWebMethodName()
{ 
   return "OK";
}

#1


2  

WebMethod officialy only can return XML or JSON. Default is json, so whatever you return gets converted to json

WebMethod官方只能返回XML或JSON。默认是json,所以无论你返回什么都转换为json

change in JQuery dataType: "json",

更改JQuery dataType:“json”,

$.ajax({
        type: "POST",
        url: "Receipt/BarcodeEntered",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
                    alert(msg);
                 }
});

and you should return class not single string. because string can not be converted valid json object.

你应该返回class而不是单个字符串。因为字符串无法转换为有效的json对象。

public class SampleClass{
    public string Message {set; get;}
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public SampleClass BarcodeEntered()
{

        return new SampleClass(){
        Message  = "Sample message"
    };

}

#2


0  

You need to return JSON, let me write down an example.

你需要返回JSON,让我写下一个例子。

public JsonResult DoStuff()
{
    string text = "text";

    return Json(text, JsonRequestBehavior.AllowGet);
}

#3


0  

Here is the working demo I am using this in my asp.net page. The data is will be in d property.

这是我在asp.net页面中使用的工作演示。数据将在d属性中。

JQuery code.

    $.ajax({
    type: "POST",
    url: "/Subfolder/MyPageName.aspx/myWebMethodName",
    data: '{}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
      if (msg.d == "OK") {
       alert("OK")
      } else {
         alert(msg.d);
      }
    }
   });

C# Code

[WebMethod]
public static string myWebMethodName()
{ 
   return "OK";
}