JSON 和 jQuery.ajax
我尝试使用 jQuery UI 自动完成功能与带有响应格式 JSON 的 Web 服务进行通信,但我无法这样做。
我的网络服务甚至没有执行,路径应该是正确的,因为错误消息没有抱怨这一点。
让我印象深刻的是标头,响应是soap,但请求是json,它应该是这样的吗?
Response Headersvisa källkod
Content-Type application/soap+xml; charset=utf-8
Request Headersvisa källkod
Accept application/json, text/javascript, */*
Content-Type application/json; charset=utf-8
我收到的错误消息如下(很抱歉消息太大,但这可能很重要):
soap:ReceiverSystem.Web.Services.Protocols.SoapException:服务器无法处理请求。 ---> System.Xml.XmlException:根级别的数据无效。第 1 行,位置 1。 在 System.Xml.XmlTextReaderImpl.Throw(异常 e) 在 System.Xml.XmlTextReaderImpl.Throw(字符串 res,字符串 arg) 在 System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() 在 System.Xml.XmlTextReaderImpl.ParseDocumentContent() 在 System.Xml.XmlTextReaderImpl.Read() 在 System.Xml.XmlTextReader.Read() 在 System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() 在 System.Xml.XmlReader.MoveToContent() 在 System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent() 在 System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement() 在 System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest() 在 System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage 消息) 在 System.Web.Services.Protocols.SoapServerProtocol.Initialize() 在 System.Web.Services.Protocols.ServerProtocolFactory.Create(类型类型、HttpContext 上下文、HttpRequest 请求、HttpResponse 响应、Boolean& abortProcessing) --- 内部异常堆栈跟踪结束 ---
这是我的代码:
$('selector').autocomplete({
source: function(request, response) {
$.ajax({
url: "../WebService/Member.asmx",
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST",
data: JSON.stringify({prefixText: request.term}),
success: function(data) {
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('error');
}
})
},
minLength: 1,
select: function(event, ui) {
}
});
我的 Web 服务如下所示:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Member : WebService
{
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] GetMembers(string prefixText)
{
code code code
}
}
我做错了什么?提前致谢 :)
im trying to use the jQuery UI autocomplete to communitate with a webservice with responseformate JSON, but i am unable to do so.
My webservice is not even executed, the path should be correct since the error message does not complain about this.
What strikes me is the headers, response is soap but request is json, is it supposed to be like this?
Response Headersvisa källkod
Content-Type application/soap+xml; charset=utf-8
Request Headersvisa källkod
Accept application/json, text/javascript, */*
Content-Type application/json; charset=utf-8
The error message i get is as follows (sorry for the huge message, but it might be of importance):
soap:ReceiverSystem.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
at System.Xml.XmlReader.MoveToContent()
at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
--- End of inner exception stack trace ---
This is my code:
$('selector').autocomplete({
source: function(request, response) {
$.ajax({
url: "../WebService/Member.asmx",
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST",
data: JSON.stringify({prefixText: request.term}),
success: function(data) {
alert('success');
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('error');
}
})
},
minLength: 1,
select: function(event, ui) {
}
});
And my webservice looks like this:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Member : WebService
{
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string[] GetMembers(string prefixText)
{
code code code
}
}
What am i doing wrong? Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您需要在 URL 中包含方法的名称:
/WebService/Member.asmx/GetMembers
当使用 Web 服务时,我总是喜欢将路径设置为根目录(/ no ../),如果您正在执行任何类型的 URL 重写或使用路由。
这是一篇关于从 JQuery 调用 Web 服务的优秀博客文章,值得一读。 http://encosia.com/ 2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
I believe you need to include the name of the method to your URL:
/WebService/Member.asmx/GetMembers
When using a webservice I always like to set the path to the root (/ no ../), it can be helpful if you are doing any kind of URL rewrites or using routing.
Here is a good blog post to read in regards to calling a webservice from JQuery. http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/