2012-07-30 2 views
1

다음 C#/jQuery를 사용하여 500을 계속 유지합니다. 주어진 구현은 옳지 않을 수 있으며 큰 문제는 아닙니다. 나는 단지 안녕하세요 세상을 얻으려고하고 있습니다.ASP.NET hello world AJAX 게시물

[WebMethod()] 
public static string Test(string data) 
{ 
    // never gets here 
    return "hello world"; 
} 

$.ajax({ 
    type: "POST", 
    url: "ajax.aspx/Test", 
    data: "data: {data:'abc'}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (data) { 
     alert("back"); 
    } 
}); 
+0

어떻게'데이터 '를 설정합니까? data : "{data : '"+ data + "'}"' –

+0

데이터 변수는 따옴표가없는 문자열입니다. 즉 데이터가 -> abcdefg –

+0

해당 컨트롤러 메소드를 사용하려고 시도하고있는 URL은 무엇입니까? –

답변

1

MVC를 사용하지 않아도 작동한다고 생각합니다. 나는 당신이 json 매개 변수를 전달하는 방식이 잘못되었다고 생각합니다. 아래 코드를 확인하고 작동 여부를 알려주십시오.

[WebMethod()] 
public static string Test(string data) 
{ 
    // never gets here 
    return "hello world"; 
} 

$.ajax({ 
    type: "POST", 
    url: "ajax.aspx/Test", 
    data:'{"data":"abc"}', 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (response) { 
     alert(response); 
    } 
}); 
+0

마지막으로 그것을 사용하여 작업하게 만들었습니다. [WebMethod]는 괄호없이 작동했습니다. –

0

을 시도하십시오는 C#이 인수가 없지만 최대한 빨리 데이터를받을하려고으로는 500

[WebMethod] 
public static string Test(string s) 
{ 
    // never gets here 
} 

$.ajax({ 
    type: "POST", 
    url: "ajax.aspx/" + method, 
    /*async: true,*/ 
    data: "{data:'" + data + "'}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (data) { 
     callback(data.d); 
    } 
}); 

최신 시도가이 어떤 여전히 나던 작품이다주는 경우 작동

[HttpPost] 
public ActionResult Test(string x) 
{ 
    // never gets here 
    return Json(true) 
} 

$.ajax({ 
    type: "Post", 
    url: "ajax/Test", 
    data: {x:'abc'}, 
    dataType: "json", 
    success: function (data) { 
     alert("back"); 
    } 
}); 
+0

여전히 작동하지 않습니다 : –

+0

확실하지 않지만 매개 변수 이름을 변경하십시오. 데이터 : {s : "abc"} ' –

+0

리소스를로드하지 못했습니다 : 서버가 500 (내부 서버 오류) http : // 상태로 응답했습니다. localhost : 16679/ajax.aspx/Test –