当本地webservice调用时,JSON响应返回null [英] JSON response returning null when local webservice called
问题描述
大家好,
我在asp.net上做了一个简单的web服务,我的service.asmx文件包含webmethod:
Hello everyone,
I have made a simple webservice in asp.net, my service.asmx file contains webmethod as :
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void HelloWorld() {
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonData = serializer.Serialize("Hello World");
Context.Response.ContentType = "application/json; charset=utf-8";
Context.Response.Write(jsonData);
//return jsonData;
}
这个webservice返回Hello World的输出
现在我已经在IIS上托管了这个web服务从localhost访问这个web服务也很好。
现在我在asp.net中创建了一个网站,其中有一个按钮通过AJAX和JSON调用这个web服务
代码为:
This webservice returns me output of "Hello World"
Now i have hosted this webservice on IIS and accessing this webservice from localhost also works very fine.
Now i have created one website in asp.net in which there is a button which invokes this webservice through AJAX and JSON
The code is :
function invokeService() {
$.ajax({
url: "http://localhost/SampleService/Service.asmx/HelloWorld",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg);
},
error: function () {
alert("this error");
}
});
return false;
}
<input type="button" id="btnInvokeService" value="Invoke Service" onclick ="invokeService();" />
现在调用invokeService方法的jSON,但我返回的输出是null值..
我也尝试用返回的字符串值更改webservice webmethod,但最终结果是一样的。
请帮助我如何实现输出JSON提醒为Hello world ..请帮帮我
提前感谢您。
Krunal
Now the invokeService method is called of jSON but the output i am returning is "null" value..
I have also tried to change webservice webmethod with returning string value but the end result is same.
Please help me how can i achieve output in JSON alert as Hello world.. Please help me
Thanking you in advance.
Krunal
推荐答案
.ajax({
url:http://localhost/SampleService/Service.asmx/HelloWorld,
类型:POST ,
contentType:application / json; charset = utf-8,
dataType:json,
success:function(msg){
alert(msg);
},
错误:function(){
alert(this error);
}
});
返回false;
}
< input type =buttonid =btnInvokeServicevalue =Invoke Serviceonclick =invokeService(); />
.ajax({ url: "http://localhost/SampleService/Service.asmx/HelloWorld", type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { alert(msg); }, error: function () { alert("this error"); } }); return false; } <input type="button" id="btnInvokeService" value="Invoke Service" onclick ="invokeService();" />
现在调用invokeService方法的jSON,但我返回的输出是null值..
我也尝试用返回的字符串值更改webservice webmethod,但最终结果是一样的。
请帮助我如何实现输出JSON提醒为Hello world ..请帮帮我
提前感谢您。
Krunal
Now the invokeService method is called of jSON but the output i am returning is "null" value..
I have also tried to change webservice webmethod with returning string value but the end result is same.
Please help me how can i achieve output in JSON alert as Hello world.. Please help me
Thanking you in advance.
Krunal
我建议将你的AJAX调用URL更改为:
I would recommend changing your AJAX call URL to this:
url: "Service.asmx/HelloWorld",
发布到IIS时对本地主机的引用不会很好。
将您的服务更改为:
The reference to the local host would not bode well when published to IIS.
Change your service to this:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld() {
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonData = serializer.Serialize("Hello World");
Context.Response.ContentType = "application/json; charset=utf-8";
Context.Response.Write(jsonData);
return jsonData;
}
这篇关于当本地webservice调用时,JSON响应返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!