I 너무MVC3 컨트롤러 널 파라미터
public JsonResult GetById(Guid id)
{
var results = from a in repository.AsQueryable<Department>()
where a.Id == id
orderby a.Name
select new { id = a.Id, name = a.Name };
return Json(results, JsonRequestBehavior.AllowGet);
}
같은 가이 파라미터를 허용하는 매우 단순한 제어 방법을 가지고 json.stringify 사용 JSON.stringify() 크롬, IE 및 사용시 파라미터 항상 널 Firefox. 예 ...
$(document).ready(function() {
var o = new Object();
o.id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
$.getJSON('@Url.Action("GetById", "User")', JSON.stringify(o), function() {
alert('Completed');
});
});
이전에는 ASP.NET 4.0에서 작동했습니다. 이상한 점은 다음과 같은 것이 효과가 있다는 것입니다.
$(document).ready(function() {
$.getJSON('@Url.Action("GetById", "User")', { "id": "C21803C3-1385-462E-ACEA-AFA1E554C635" }, function() {
alert('Completed');
})
.error(function (a, b, c) {
alert(a.responseText); alert(b); alert(c);
});
});
내가 실행하는 경우
...$(document).ready(function() {
var o = new Object();
o.id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
alert(JSON.stringify(o));
});
나는
{"id":"C21803C3-1385-462E-ACEA-AFA1E554C635"}
표시, 적절한 JSON을 얻을. 그리고 내가
$(document).ready(function() {
var o = new Object();
o.id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
var json_text = JSON.stringify(o, null, 2);
alert(json_text);
var your_object = JSON.parse(json_text);
alert(your_object.id);
});
를 실행하면 내가 아약스 게시물로 이것을 시도
C21803C3-1385-462E-ACEA-AFA1E554C635
추가 참고, 같은 문제를 얻을 경우.
제한된 게시물을 보았지만 아래에서 전체 작업을 참조하십시오.
JSON.stringify (o, null, 2) 같은 공백으로 공백을 삽입하려했습니다.
jquery-1.7.1.min.js, jquery-ui-1.8.16.custom.min.js, jquery.unobtrusive-ajax.js, jquery.validate.min.js 및 jquery.validate를 사용하십시오. unobtrusive.min.js. 유일한 다른 JS는 자바 스크립트가 활성화되어 있고 jQuery 대화 상자에서 폼을 열고 클릭 가능한 테이블을 만드는 것입니다.
$.ajaxSetup({ cache: false });
$(document).ready(function() {
$(".openDialog").live("click", function (e) {
e.preventDefault();
$("<div></div>")
.addClass("dialog")
.attr("id", $(this).attr("data-dialog-id"))
.appendTo("body")
.dialog({
title: $(this).attr("data-dialog-title"),
close: function() { $(this).remove() },
modal: true
})
.load(this.href);
});
$(".close").live("click", function (e) {
e.preventDefault();
$(this).closest(".dialog").dialog("close");
});
var clickableTable = $('tr[data-tr-clickable-url]');
if (clickableTable.length > 0) {
clickableTable.addClass('clickable') // Add the clickable class for mouse over
.click(function() {
window.location.href = $(this).attr('data-tr-clickable-url');
});
// Remove the last child, containing anchors to actions, from each row, including the header.
$('tr :last-child').remove();
}
});
UPDATE
다음 작품 :
var o = new Object();
o.Id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
$.ajax({
url: '@Url.Action("GetById", "User")',
type: "POST",
data: JSON.stringify(o),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function() {
alert('completed');
}
});
다음은 작동하지 않습니다
이var o = new Object();
o.Id = 'C21803C3-1385-462E-ACEA-AFA1E554C635';
$.ajax({
url: '@Url.Action("GetById", "User")',
data: JSON.stringify(o),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function() {
alert('completed');
}
});
은 따라서 유형으로 POST를 제거하면 실패 할 호출됩니다. jQuery 문서에 따르면 $ .getJSON은
$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});
과 동일합니다. 유형이 정의되어 있지 않음에 유의하십시오. 정확히 어디서 오류가 있는지는 모르지만, 어딘가에 놓친 것이 있습니다. 특히 $ .getJSON에 실제 JSON 객체를 전달하면 실제로 작동합니다.
을 구현할 수 있습니다 ' C21803C3-1385-462E-ACEA-AFA1E554C635 '}; –
의심되는 것처럼 작동합니다. 그것은 JSON.stringify()로 제한된 것 같습니다. – user1097974
동일한 문제가 발생했지만 jQuery.post()를 사용하고있었습니다. 나는 나의 다른 대답을 지웠다. 그리고 당신은 당신이 모든 등장 인물들을 탈출 할 수 있다고 지적했다. jQuery가 당신을 위해 그것을하고있는 것을 볼 수 있습니다. 예 : {id : "hello", input : "id = test"}는이 HTTP 요청으로 변환됩니다 : id = hello & input = id % 3Dtest –