简体   繁体   English

jQuery ajax函数中contentType和dataType之间的差异

[英]Differences between contentType and dataType in jQuery ajax function

I have the following Jquery callback function and I have a litle doubt about it (I don't know very well Jquery): 我有以下Jquery回调函数,我有一个小问题(我不太了解Jquery):

$("form.readXmlForm").submit(function() {
    // Riferimento all'elemento form che ha scatenato il submit 
    var form = $(this);
    // Variabile che contiene il riferimento al bottone clickato 
    var button = form.children(":first");

    $.ajax({        // Viene eseguita la chiamata AJAX 
        type: "POST", // Tipo di richiesta: POST 
        // URL verso quale viene inviata la richiesta
        url: form.attr("action"),    
        // Dati XML inviati: 
        data: "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><javaBean><foo>bar</foo><fruit>apple</fruit></javaBean>", 
        // Tipo di media type accettabile dalla response: 
        contentType: "application/xml", 
        dataType: "text", 

        success: function(text) { 
            MvcUtil.showSuccessResponse(text, button); 
        }, 

        error: function(xhr) { 
            MvcUtil.showErrorResponse(xhr.responseText, button); 
        }
    });

As you can see this function simply execute an AJAX Request to the backend setting the parameter for this request. 正如您所看到的,此函数只是对后端执行AJAX请求,为此请求设置参数。

I have set that I am sending the request towards an URL, that the request is a POST request and that the data that I am sending are the following string: 我已经设置了我向URL发送请求,请求是POST请求,并且我发送的数据是以下字符串:

"barapple" “barapple”

I have some difficulties to understand what is the differences between contentType and dataType 我很难理解contentTypedataType之间的区别

I think that contentType specify the type of data that are acceptable recived in the HTTP Response, is it right? 我认为contentType指定了HTTP响应中可接受的数据类型,是不是?

And dataType? 和dataType? What say? 说啥? The type of data that I am sending in the HTTP Request? 我在HTTP请求中发送的数据类型?

In this case is is "text" because I am sending a textual string that rappresent XML code? 在这种情况下是“文本”,因为我发送的文本字符串是rappresent XML代码?

From the documentation : 文档

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') contentType(默认值:'application / x-www-form-urlencoded; charset = UTF-8')

Type: String 类型:字符串

When sending data to the server, use this content type. 将数据发送到服务器时,请使用此内容类型。 Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. 默认为“application / x-www-form-urlencoded; charset = UTF-8”,这在大多数情况下都适用。 If you explicitly pass in a content-type to $.ajax(), then it'll always be sent to the server (even if no data is sent). 如果您明确地将内容类型传递给$ .ajax(),那么它将始终发送到服务器(即使没有数据发送)。 If no charset is specified, data will be transmitted to the server using the server's default charset; 如果未指定charset,则使用服务器的默认字符集将数据传输到服务器; you must decode this appropriately on the server side. 你必须在服务器端适当地解码它。

and: 和:

dataType (default: Intelligent Guess (xml, json, script, or html)) dataType(默认值:Intelligent Guess(xml,json,script或html))

Type: String 类型:字符串

The type of data that you're expecting back from the server. 您期望从服务器返回的数据类型。 If none is specified, jQuery will try to infer it 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). 如果没有指定,jQuery将尝试根据响应的MIME类型推断它(XML MIME类型将产生XML,在1.4 JSON中将产生一个JavaScript对象,在1.4脚本中将执行脚本,其他任何东西将是以字符串形式返回)。

They're essentially the opposite of what you thought they were. 它们基本上与你的想法相反。

在此输入图像描述

In English: 用英语讲:

  • ContentType : When sending data to the server, use this content type. ContentType :将数据发送到服务器时,请使用此内容类型。 Default is application/x-www-form-urlencoded; charset=UTF-8 默认为application/x-www-form-urlencoded; charset=UTF-8 application/x-www-form-urlencoded; charset=UTF-8 , which is fine for most cases. application/x-www-form-urlencoded; charset=UTF-8 ,对大多数情况来说都没问题。
  • Accepts : The content type sent in the request header that tells the server what kind of response it will accept in return. Accepts :请求标头中发送的内容类型告诉服务器它将接受哪种响应。 Depends on DataType . 取决于DataType
  • DataType : The type of data that you're expecting back from the server. DataType :您期望从服务器返回的数据类型。 If none is specified, jQuery will try to infer it based on the MIME type of the response. 如果没有指定,jQuery将尝试根据响应的MIME类型推断它。 Can be text, xml, html, script, json, jsonp . 可以是text, xml, html, script, json, jsonp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM