AJAX 요청에서 컨텐츠 유형 및 데이터 유형은 무엇입니까?
POST 요청에서 컨텐츠 유형 및 데이터 유형은 무엇입니까? 내가 이것을 가지고 있다고 가정 해보십시오.
$.ajax({
type : "POST",
url : /v1/user,
datatype : "application/json",
contentType: "text/plain",
success : function() {
},
error : function(error) {
},
가 contentType
우리가 보낼거야? 위의 예에서 우리가 보내는 것은 JSON이고 우리가받는 것은 일반 텍스트입니까? 나는 정말로 이해하지 못한다.
contentType
보내는 데이터의 유형이므로 기본값 application/json; charset=utf-8
인 그대로있는 것이 일반적 application/x-www-form-urlencoded; charset=UTF-8
입니다.
dataType
서버에서 다시 기대하고 무엇 : json
, html
, text
, 등 jQuery를이 성공 함수의 매개 변수를 채우는 방법을 알아 내기 위해이 사용됩니다.
다음과 같은 것을 게시하는 경우 :
{"name":"John Doe"}
그리고 다시 기대 :
{"success":true}
그럼 당신은해야합니다 :
var data = {"name":"John Doe"}
$.ajax({
dataType : "json",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(data),
success : function(result) {
alert(result.success); // result is an object which is created from the returned JSON
},
});
다음을 기대하는 경우 :
<div>SUCCESS!!!</div>
그런 다음 수행해야합니다.
var data = {"name":"John Doe"}
$.ajax({
dataType : "html",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(data),
success : function(result) {
jQuery("#someContainer").html(result); // result is the HTML text
},
});
하나 더-당신이 게시하려는 경우 :
name=John&age=34
그런 다음 stringify
데이터를 하지 말고 다음을 수행 하십시오.
var data = {"name":"John", "age": 34}
$.ajax({
dataType : "html",
contentType: "application/x-www-form-urlencoded; charset=UTF-8", // this is the default value, so it's optional
data : data,
success : function(result) {
jQuery("#someContainer").html(result); // result is the HTML text
},
});
From the jQuery documentation - http://api.jquery.com/jQuery.ajax/
contentType When sending data to the server, use this content type.
dataType 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
"text": A plain text string.
So you want contentType to be application/json
and dataType to be text
:
$.ajax({
type : "POST",
url : /v1/user,
dataType : "text",
contentType: "application/json",
data : dataAttribute,
success : function() {
},
error : function(error) {
}
});
See http://api.jquery.com/jQuery.ajax/, there's mention of datatype and contentType there.
They are both used in the request to the server so the server knows what kind of data to receive/send.
참고URL : https://stackoverflow.com/questions/18701282/what-is-content-type-and-datatype-in-an-ajax-request
'development' 카테고리의 다른 글
MATLAB에 foreach가 있습니까? (0) | 2020.05.30 |
---|---|
ZXing을 사용하여 Android 바코드 스캔 앱 만들기 (0) | 2020.05.30 |
단일 개정의 자식 로그 (0) | 2020.05.30 |
Task.FromResult의 용도는 무엇입니까 (0) | 2020.05.30 |
C에서 어떤 사람들은 포인터를 해제하기 전에 포인터를 던지는 이유는 무엇입니까? (0) | 2020.05.30 |