简单的jQuery ajax问题是否成功返回数据? [英] Simple jQuery ajax questions with success return data?

查看:93
本文介绍了简单的jQuery ajax问题是否成功返回数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

         $.ajax({
           type: "POST",
           url: global.pathPrefix + '/services/brochure.asmx/ShowPostRequest',
           data: "parkIds=" + $('input.ids').val(),               
           success: function(msg){
             alert( "Data Saved: " + msg );
           },
           error: function(msg){
             alert( "error: " + msg );
           }               
         });

在获取"[object Document]"时,我期望为"true"或"false".如何解析返回的数据?在jQuery网站上,没有太多要了解的内容.我也尝试过"msg.d",但未返回任何内容.

I am expecting "true" or "false" while I'm getting "[object Document]". How to parse this returned data? on the website of jQuery, there are not much to learn about. I've tried "msg.d" as well which returned nothing.

谢谢

推荐答案

使用jQuery的.ajax函数时,它传递回回调的内容取决于发送的响应类型.来自文档:

When you use jQuery's .ajax function, what it passes back to your callbacks depends on what kind of response is sent. From the docs:

jQuery将根据响应的MIME类型智能地尝试获取结果(XML MIME类型将产生XML,在1.4中,JSON将产生JavaScript对象,在1.4脚本中,脚本将执行该脚本,其他所有内容将返回为字符串).

jQuery will intelligently try to get the results, based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).

从您引用的错误消息来看,您的服务器端脚本似乎正在返回XML,因此jQuery正在将其转换为XML文档实例.如果需要,可以使用请求中的dataType选项(例如,dataType: "text")覆盖jQuery的猜测并告诉其如何处理响应,但是,当然,如果返回的是XML,则您的警报将显示完整的XML,而不仅仅是您所追求的特定成功元素的内容.

From the error message you've quoted, it looks like your server-side script is returning XML, and so jQuery is turning that into an XML document instance for you. If you want, you can override jQuery's guessing and tell it how to handle the response, by using the dataType option in the request (dataType: "text", for instance), but of course, if what's coming back is XML, your alerts will show the full XML, not just the content of the particular success element you're after.

更有可能,您实际上想使用返回的XML文档,毕竟这是一个很好的结构化文档. jQuery常用的遍历内容可用于在XML结构中四处移动.例如,假设您的回复如下所示:

More likely, you want to actually use the XML document that was returned, which is after all a nice structured document. jQuery's usual traversal stuff can be used to move around within the XML structure. For instance, let's assume your response looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<foo>
    <success>true</success>
</foo>

然后您的success函数可以执行以下操作:

Then your success function could do this:

success: function(xml) {
    var success = $(xml).find('success').text();
    alert(success);
}

以下是有关在jQuery中使用XML的文章:

Here are some articles about using XML with jQuery:

  • XML Parsing with jQuery
  • jQuery and XML
  • Reading XML with jQuery

这篇关于简单的jQuery ajax问题是否成功返回数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆