2013-06-13 55 views
0
 $.ajax(
     { 
      type: 'GET', 
      url: 'ProductOp.aspx/getProduct', 
      data: '1', 
      contentType: 'application/json; charset=utf-8', 
      dataType: 'json', 
      success: function (msg) { 
       oldProduct = JSON.parse(msg.d); 
      } 
     }); 

    [WebMethod] 
    [ScriptMethod(UseHttpGet = true)] 
    public static string getProduct(string ID) 
    { 
     ProductOperations productOp = new ProductOperations(); 
     ProductObject product = productOp.Read(Convert.ToInt32(JsonConvert.DeserializeObject(ID))); 
     return JsonConvert.SerializeObject(product); 
    } 

給我Web方法阿賈克斯獲得給予500內部服務器錯誤

消息 「無效的Web服務調用,缺少值參數:‘ID’」 堆棧跟蹤

" konum: System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters) konum: System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters) konum: System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams) konum: System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)" 

ExceptionType 「System.InvalidOperationException」

預先感謝您

回答

3

添加標識PARAM數據:

$.ajax(
    { 
     type: 'GET', 
     url: 'ProductOp.aspx/getProduct', 
     data: { ID: 1 }, 
     contentType: 'application/json; charset=utf-8', 
     dataType: 'json', 
     success: function (msg) { 
      oldProduct = JSON.parse(msg.d); 
     } 
    }); 
+0

非常感謝你這做了最後一個問題如果我想傳遞2個參數將「數據:{ID:1,名稱:ASD}」工作? –

+0

是的,這將工作,但對於字符串使用單引號,你可以這樣做,數據:{ID:1,名稱:'asd'},請不要忘記標記爲答案:) –

2
$.ajax(
    { 
     type: 'GET', 
     url: 'ProductOp.aspx/getProduct', 
     data: '{ID:'1'}', 
     contentType: 'application/json; charset=utf-8', 
     dataType: 'json', 
     success: function (msg) { 
      oldProduct = JSON.parse(msg.d); 
     } 
    }); 
0
var obj = "yhdgfhgfh"; 

$.ajax({ 
     type: "GET", 
     url: Url, 
     data: { data: "hggfh" }, 
     contentType: "application/json; charset=utf-8", 
     // data: "{'data' : '" + obj + "'}", 

     dataType: "json", 

}); 
相關問題