jQuery ajax 发布到 Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3099369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:52:23  来源:igfitidea点击:

jQuery ajax post to web service

jqueryxmlajaxweb-servicesashx

提问by Jan Johansen

$(document).ready(function() {
        $.ajax({ type: "POST",
                        url: "/getprojects.ashx",
                        data: "<formData client=\"\" year=\"\" categories=\"\" tags=\"\" freeText=\"\" count=\"34\" page=\"1\"></formData>",
                        dataType: "text/xml",
                        cache: false,
                        error: function() { alert("No data found."); },
                        success: function(xml) {
                            alert("it works");
                            alert($(xml).find("project")[0].attr("id"));
                        }
        });
    });

My problem is i get some data back but i can't seem to get it displayed.

我的问题是我取回了一些数据,但似乎无法显示。

回答by Claude Vedovini

dataTypeshould be the type of what you receive but contentTypeshould be the mime-type of what you are sending, the following should be ok:

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

$(document).ready(function() {
        $.ajax({ type: "POST",
                        url: "/getprojects.ashx",
                        data: "<formData client=\"\" year=\"\" categories=\"\" tags=\"\" freeText=\"\" count=\"34\" page=\"1\"></formData>",
                        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"));
                        }
        });
    });

回答by jAndy

Your dataTypeseems to be wrong. It should look like

你的dataType好像错了。它应该看起来像

dataType: "xml"

Your datastructure also looks pretty wierd. Have a look at .serializeArray(). It should be standard query string foo=bar&test=blaetc.

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

If the success handlergets executed, try to lookup your xmlvariable plain, without operating on it with .find()or whatever. Still empty?

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