ASP.NET jQuery Ajax 호출 코드 비하인드 메서드
저는 웹 개발이 처음이지만 전반적으로 개발 경험이 많습니다.입력 필드가 몇 개 있고 제출 버튼이 있는 ASP 페이지가 있습니다.이 제출 버튼은 순수하게 $.ajax를 호출합니다. 코드 뒤 파일에서 메서드를 호출하려고 했습니다.하지만 두 가지 흥미로운 점을 발견했습니다.첫째, 아약스 콜은 어떤 데이터를 제공받든 상관없이 성공합니다.둘째, responseText 필드는 페이지 전체의 html 소스입니다.
이 글과 웹 구성을 가리키는 다른 글들을 읽었지만, 이 해결책들은 내 문제를 해결해 주지 못하는 것 같습니다.
asp 페이지는 다음과 같습니다.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="TesScript.js"></script>
<link rel="Stylesheet" type="text/css" href="TestStyle.css" />
</head>
<body>
<div>
<ul class="tempList">
<li>Name:
<input id="nameText" type="text" />
</li>
<li>Attending:
<input id="yesButton" type="radio" name="attending" />
Yes
<input id="noButton" type="radio" name="attending" />
No </li>
<li>Return Address:
<input id="returnAddressText" type="text" />
</li>
<li>
<input id="submitButton" type="button" onclick="submit()" value="Submit" />
</li>
</ul>
</div>
<ul id="errorContainer" class="errorSection" runat="server" />
<ul id="messageContainer" class="messageSection" runat="server" />
</body>
</html>
뒤에 있는 코드:
using System;
using System.Web.Services;
using System.Web.UI;
namespace TestAspStuff
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string OnSubmit(string name, bool isGoing, string returnAddress)
{
return "it worked";
}
}
}
그리고 자바스크립트:
function submit() {
var name = "my name";
var isAttending = true;
var returnAddress = "myEmail@gmail.com";
SendMail(name, isAttending, returnAddress);
}
function SendMail(person, isAttending, returnEmail) {
var dataValue = { "name": person, "isGoing": isAttending, "returnAddress": returnEmail };
$.ajax({
type: "POST",
url: "Default.aspx/OnSubmit",
data: dataValue,
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
complete: function (jqXHR, status) {
alert("complete: " + status + "\n\nResponse: " + jqXHR.responseText);
}
});
}
이제 url 속성을 원하는 대로 변경할 수 있고 오류 방법이 호출되지 않으며 상태는 성공이며 응답 텍스트는 html 페이지 전체입니다.내 웹 구성에는 모든 적절한 섹션(htmlModule 섹션 포함)이 있습니다.저는 일을 하고 있습니다.순 3.5.도움을 주시면 감사하겠습니다. 그리고 다시 말하지만, 저는 이 일이 처음이라 다른 사람들에게 명백한 것은 저에게 분명하지 않을 가능성이 높습니다.그리고 이것을 하는 더 좋은 방법이 있다면(JavaScript에서 calling asp.net 코드 비하인드 메소드, 즉) 자유롭게 올려주세요.감사합니다!!!
먼저 자바스크립트에서 Submit() 메서드의 맨 아래에 반환 false를 추가할 수 있습니다(AJAX에서 처리하므로 제출이 중지됩니다).
성공 이벤트가 아닌 전체 이벤트에 연결하는 것입니다. 이는 상당한 차이가 있기 때문입니다.또한 서명 방법을 귀하와 일치시킨 적이 없으며 항상 컨텐츠를 제공했습니다.Type 및 dataType입니다.예를 들어,
$.ajax({
type: "POST",
url: "Default.aspx/OnSubmit",
data: dataValue,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
success: function (result) {
alert("We returned: " + result);
}
});
이것도 제 문제를 해결하지 못해서 파라미터를 조금 바꿨습니다.
이 코드는 내게 효과가 있었습니다.
var dataValue = "{ name: 'person', isGoing: 'true', returnAddress: 'returnEmail' }";
$.ajax({
type: "POST",
url: "Default.aspx/OnSubmit",
data: dataValue,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
success: function (result) {
alert("We returned: " + result.d);
}
});
오래된 실을 알고 있습니다.한 가지 가능한 해결책은JSON.stringify( )
적절한 JSON 문자열로 변환합니다.이렇게 하면 관련된 모든 데이터 매개 변수 값 문제가 해결됩니다.
function SendMail(person, isAttending, returnEmail) {
var dataValue = { "name": person, "isGoing": isAttending, "returnAddress": returnEmail };
$.ajax({
type: "POST",
url: "Default.aspx/OnSubmit",
data: JSON.stringify(dataValue),
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
complete: function (jqXHR, status) {
alert("complete: " + status + "\n\nResponse: " + jqXHR.responseText);
}
});
}
언급URL : https://stackoverflow.com/questions/18236634/asp-net-jquery-ajax-calling-code-behind-method
'it-source' 카테고리의 다른 글
JQuery는 다른 자바스크립트에서 AJAX 호출을 들을 수 있습니까? (0) | 2023.10.28 |
---|---|
구조체에 대한 모든 포인터가 동일한 크기여야 하는 이유는 무엇입니까? (0) | 2023.10.28 |
Oracle sql 병합을 삽입 및 삭제하지만 업데이트하지 않음 (0) | 2023.10.28 |
bloginfo('template_url') 또는 echo esc_url( get_template_directory_uri()) 중 템플릿 디렉토리의 URL을 검색하는 더 나은 방법은 무엇입니까? (0) | 2023.10.28 |
HTML 텍스트 영역에 줄 바꿈을 추가하는 방법 (0) | 2023.10.28 |