2010-04-09 5 views
1

jquery ajax를 통해 호출하는 서버 메소드에 입력 텍스트에 입력 된 문자열을 전달해야합니다. 하지만 그게 끝나지 않을거야. 누가 내가 여기서 잘못하고 있는지 말해 줄 수있어. 아래 코드는 다음과 같습니다ajax 호출에 입력 텍스트 값을 전달합니다.

$.ajaxSetup({ cache: false timeout: 1000000}); function concatObject(obj) { strArray = []; //new Array for (prop in obj) {  strArray.push(prop + " value :" + obj[prop]); } return strArray.join();} //var Eid = "stephen.gilroy1"; function testCAll() { //var ntid = $('#Eid').val();  $.ajax({  type: "POST",  url: "Testing.aspx/SendMessage",  //data: "{'ntid':'stephen.gilroy1'}",  //working  data: "{'ntid': $('#Eid').val()}",  contentType: "application/json; charset=utf-8",  dataType: "json",  success: function(result) {   alert(result.d);   resultData = eval("(" + result.d + ")");   $("#rawResponse").html(result.d);   //$("#response").html(resultData.sn);  },  error: function(result) {   alert("jQuery Error:" + result.statusText);  } });}$.ajaxSetup({ 
    cache: false 
    //timeout: 1000000 
}); 
function concatObject(obj) { 
    strArray = []; //new Array 
    for (prop in obj) { 
     strArray.push(prop + " value :" + obj[prop]); 
    } 
    return strArray.join(); 
} 

//var Eid = "stephen.gilroy1"; 

function testCAll() { 
    //var ntid = $('#Eid').val(); 
    $.ajax({ 
     type: "POST", 
     url: "Testing.aspx/SendMessage", 
     //data: "{'ntid':'stephen.gilroy1'}",  //working 
     data: "{'ntid': $('#Eid').val()}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(result) { 
      alert(result.d); 
      resultData = eval("(" + result.d + ")"); 
      $("#rawResponse").html(result.d); 
      //$("#response").html(resultData.sn); 
     }, 
     error: function(result) { 
      alert("jQuery Error:" + result.statusText); 
     } 
    }); 
} 

것은 위의 js 파일이며, 아래의 영문 파일입니다 : 당신은 따옴표에서 data: "{'ntid': $('#Eid').val()}",를 취할 필요가

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Testing.aspx.cs" Inherits="Testing" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script src="jquery.js" type="text/javascript"></script> 

    <script src="Testing.js" type="text/javascript"></script> 

    <script src="json2.js" type="text/javascript"></script> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    Employee's NTID: <input type="text" id = "Eid" name="Employee_NTID" /> 
     <asp:GridView ID="GridView1" runat="server"> 
     </asp:GridView> 
     <br /> 
<br /> 
    <input type="button" onclick="testCAll()" value = "Search"/> 

     <div id="rawResponse"></div> 
     <hr /> 
     <div id="response"></div> 

    </div> 

    </form> 
</body> 
</html> 

답변

2

.

EDIThere 코드를 보시면 문제의 코드가 올바르게 ajax 요청을 보내려고합니다.

EDIT 2는 여기 있습니다 :

$.ajax({ 
    type: "POST", 
    url: "Testing.aspx/SendMessage", 
    data: { ntid: $('#Eid').val()}, // Notice the lack of quotes 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(result) { 
     console.log(result.d); 
     resultData = eval("(" + result.d + ")"); 
     //$("#rawResponse").html(result.d); 
    }, 
    error: function(result) { 
     alert("jQuery Error:" + result.statusText); 
    } 
}); 
+0

감사하지만 문제는 아직 여기 – amby

+0

@amby입니다 : 의이 줄을 사용하여 현재 작업 특별히 작동하지 않는 것. 내가 제공 한 링크가 보여 주듯이, AJAX 콜은 제대로 해고되고 있습니다. – R0MANARMY

+0

"{ 'ntid': 'stephen.gilroy1'}", 과 같은 간단한 문자열을 보내려고했기 때문에 내 코드가 아약스 요청을 성공적으로 전송했음을 알고 있습니다. 입력 텍스트 상자 문자열 다음 그것은 jquery를 제공합니다 : 내부 서버 오류 – amby

0

데이터가 바로 포맷되지 않았습니다. json 객체를 ajax() 함수 외부에서 생성하면 관리하기 쉽습니다.

var keyvalue = { 
     ntid : $('#Eid').val() 
    }; 
    $.ajax({ 
      type: "POST", 
      url: "Testing.aspx/SendMessage", 
      //data: "{'ntid':'stephen.gilroy1'}",  //working 
      data: keyvalue, 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function(result) { 
       alert(result.d); 
       resultData = eval("(" + result.d + ")"); 
       $("#rawResponse").html(result.d); 
       //$("#response").html(resultData.sn); 
      }, 
      error: function(result) { 
       alert("jQuery Error:" + result.statusText); 
      } 
    }); 
+0

고맙지 만 아직 해결되지 않았습니다. (... 다른 아이디어 – amby

+0

자바 스크립트 오류가 발생하고 있습니까? –

+0

jquery 내부 서버 오류가 발생하지만 그 일은 다음과 같은 데이터를 보내려고 할 때입니다. data : "{ 'ntid': 'stepqwe1'} ", 그 작동하지만 입력 텍스트 값을 보내려고 할 때, 그 위에주는 오류 – amby

0

감사합니다!

data: "{'ntid': '"+$('#Eid').val()+"'}", 
관련 문제