[英]Web service C# returns undefined value
I tried a simple web service (just a test if the value will p0pulate on the JavaScript code). 我尝试了一个简单的Web服务(只是测试该值是否会在JavaScript代码上填充)。 I tried a very simple snippet but then it returns 'undefined'.
我尝试了一个非常简单的代码段,但随后返回“ undefined”。 Please advise.
请指教。 I tried some solutions but no luck.
我尝试了一些解决方案,但没有运气。
Here a simple code on asmx file 这是关于asmx文件的简单代码
[WebMethod]
public string HelloWorld(string param1, string param2)
{
return "Hello World" + param1 + ":" + param2;
}
Here is the code on my javascript. 这是我的JavaScript上的代码。
$.ajax({
url: "SimpleService.asmx/HelloWorld",
type: "POST",
data: {
'param1': 'value1',
'param2': 'value2'
},
success: function(response) {
alert(response.d);
}
});
I tried getting the results using response.text and response.value and I'm still getting an undefined value. 我尝试使用response.text和response.value获取结果,但仍得到未定义的值。
success: function(response) {
alert(response.text);
alert(response.value);
}
On my asmx file, I tried also replacing [WebMethod] to. 在我的asmx文件中,我也尝试将[WebMethod]替换为。
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld(string param1, string param2)
{
return "Hello World" + param1 + ":" + param2;
}
Please advise what is the correct format for getting the result from my web service. 请告知从我的Web服务获取结果的正确格式是什么。
Thank you 谢谢
You should put [ScriptService]
attribute to your service class as below 您应该将
[ScriptService]
属性放在服务类中,如下所示
[ScriptService]
public class SimpleService : System.Web.Services.WebService
After that, change your ajax call. 之后,更改您的ajax调用。 Put contentType and change data as below:
放置contentType并更改数据,如下所示:
$.ajax({
url: "SimpleService.asmx/HelloWorld",
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{ param1: 'value1', param2: 'value2' }",
success: function (response) {
alert(response.d);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.