我有ajax函数,可以发送一些字符串到webservice。
下面是ajax:
var data = "wkt=" + wkt;
$.ajax({
url: "....some path",
type: "POST",
data: data,
crossDomain: true,
dataType: "text",
success: function (response) {
alert(response);
},
error: function () {
console.log('Request Failed.');
}
});
下面是web服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class ValveService : System.Web.Services.WebService
{
[WebMethod]
public string ExecuteQuery(string wkt)
{
return "dummy!";
}
}
作为响应,我得到了这个字符串:
"<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">dummy!</string>"
而我希望得到的回应是“哑巴!”。
你知道为什么我会得到这个奇怪的响应,以及如何只获取从服务发送的字符串(在我的例子中是"dummy!")吗?
发布于 2018-07-14 02:09:03
我非常确定web服务只返回xml或json。可能有一种方法可以绕过它,在服务中设置响应类型,但我不确定。编辑:我看到Nerdi.org已经暗示了这一点。
当为dataType: 'text'
时,响应头不仅是文本,而且是Content-Type: text/xml; charset=utf-8
。
转到json (它是一个字符串)并使用它。
//var data = "wkt=" + wkt;
$.ajax({
url: "/path to/ExecuteQuery",
type: "POST",
data: JSON.stringify({ wkt: wkt }),
contentType: "application/json; charset=utf-8", // this will be the response header.
crossDomain: true,
dataType: "json",
success: function(response) {
// response is a wrapper. your data/string will be a value of 'd'.
alert(response.d);
},
error: function() {
console.log('Request Failed.');
}
});
发布于 2018-07-14 03:52:29
另一种选择:
[WebMethod]
public void ExecuteQuery(string wkt)
{
Context.Response.Output.Write("dummy " + wkt);
Context.Response.End();
}
https://stackoverflow.com/questions/51332210
复制相似问题