jQuery ajax发布到Web服务

$(document).ready(function() { $.ajax({ type: "POST", url: "/getprojects.ashx", data: "", dataType: "text/xml", cache: false, error: function() { alert("No data found."); }, success: function(xml) { alert("it works"); alert($(xml).find("project")[0].attr("id")); } }); }); 

我的问题是我得到一些数据,但我似乎无法显示它。

dataType应该是您收到的类型,但contentType应该是您发送的mime类型,以下内容应该没问题:

 $(document).ready(function() { $.ajax({ type: "POST", url: "/getprojects.ashx", data: "", contentType: "text/xml", dataType: "xml", cache: false, error: function() { alert("No data found."); }, success: function(xml) { alert("it works"); alert($(xml).find("project")[0].attr("id")); } }); }); 

您的dataType似乎是错误的。 应该是这样的

 dataType: "xml" 

您的data结构看起来也很奇怪。 看看.serializeArray() 。 它应该是标准查询字符串foo = bar&test = bla等。

如果success handler被执行,请尝试查找xml变量plain,而不使用.find()或其他任何操作。 还是空的?