contentTypeto typ wysyłanych danych, więc application/json; charset=utf-8jest to typowy typ application/x-www-form-urlencoded; charset=UTF-8, który jest domyślny.
dataTypejest co czekasz z serwera: json, html, text, itd jQuery użyje tego, aby dowiedzieć się, jak wypełnić Parametr Funkcja powodzenie tych.
Jeśli publikujesz coś takiego:
{"name":"John Doe"}
i oczekując z powrotem:
{"success":true}
Wtedy powinieneś mieć:
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
},
});
Jeśli spodziewasz się następujących rzeczy:
<div>SUCCESS!!!</div>
Następnie powinieneś zrobić:
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
},
});
Jeszcze jedno - jeśli chcesz napisać:
name=John&age=34
Więc nie stringifydanych i zrób:
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
},
});