关于jquery中ajax数据返回类型问题

.net方法

namespace Controllers
{
    public class DocumentController
    {
  
public ActionResult Index()
        {
            return View();
        }

public string GetDocumentUrl() { return Comment.documentUrl; } } }
这中方式取不到值,原来我使用dataType:'json' 而。net中的方法返回值是string类型的。改成dataType: 'Text'就好了,或者直接去掉dataType:'json' 它会自动匹配数据类型的。
dataType的解释:
dataType (String) : (默认值:智能判断xml或者html)预期服务器返回的数据类型。
如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息返回 responseXML 或 responseText,并作为回调函数参数传递,可用值: 
"text": 返回纯文本字符串
documentUrl = function () {
            var url = "0";
            $.ajax({
                type: 'post',
                url: '/Document/GetDocumentUrl',
                dataType: 'json',
                success: function (data) {
                    url = data; 
                }
            });
            return url;
        }
或者使用这种方法
$.post("/Document/GetDocumentUrl", function (data) { alert(data); });
或者加上数据类型"Text",使用"json"同样是不行的:
$.post("/Document/GetDocumentUrl", function (data) { alert(data); }, "Text");
posted @ 2011-04-26 16:06  天平盛世  阅读(2507)  评论(1编辑  收藏  举报