English 中文(简体)
jQuery ajax Post 中的内容和数据类型和数据类型是什么?
原标题:What is contentType and dataType and data in jQuery ajax Post?
  • 时间:2012-05-25 10:29:34
  •  标签:
  • c#
  • json

我刚刚开始使用 Json 向Gridgeview 学习Json 和 约束性数据, 但我无法理解什么是内容Type 和 Data Type 和 数据?

我用了下面的代码......

<script type="text/javascript">
$(document).ready(function () {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Gridview.aspx/BindDatatable",
        data: "{}",
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.d.length; i++) {
                $("#gvDetails").append("<tr><td>" + data.d[i].OfficeName + "</td><td>" + data.d[i].City + "</td><td>" + data.d[i].Country + "</td></tr>");
            }
        },
        error: function (result) {
            alert("Error");
        }
    });
});
</script>
最佳回答

内容Type 指的是用于指定服务器所设定内容类型的 MIME 内容类型。 这可以识别 FORM- Encodded、 XML、 JSON 和其他很多内容类型。 它有助于服务器决定如何处理内容 。

dataType 帮助 JQuery 处理数据。 如果指定 json, 那么返回的数据将被评估为json, 传递给成功处理器的数据将是一个对象而不是字符串

数据属性用于传送到服务器的数据 。 如果您在对象文字中通过, JQuery 将会通过它作为请求机构的一部分( 如果类型是 pos) 或作为查询字符串的一部分( 如果类型是 获取) 。

问题回答

如果我们指定数据类型为 Json, 那么返回的数据将会被评估为 Json, 传递给成功处理器的数据将会是一个对象而不是字符串, 让我们看看这个示例

  $.ajax({
        type: "POST",
        url: "ProductWebService.asmx/GetProductDetails",

    data: "{ productId : " + productId + " }",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        var Product = response.d;
        $("#spnProductId").html(Product.Id);strong text
        $("#spnProductName").html(Product.Name);
        $("#spnPrice").html(Product.Price);
        $("#outputTable").show();
    },
    failure: function (msg) {
        alert(msg);
    }
});




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签