简体   繁体   English

jQuery / ajax POST一个Array /对象到C#代码后面

[英]jQuery/ajax POST an Array / objects to C# code behind

as i try to learn through other questions , still i cant get it to work 当我尝试通过其他问题学习时,仍然无法让它发挥作用

this is my code so far , trying to be as thorough as i could get . 到目前为止,这是我的代码,试图尽可能彻底。

the event (on click) 活动(点击)

var resluts = []; //its a collections of id's - list items of unsorted list as strings 
$('#next').click(function() {
    var RLength = resluts.length;
    alert(resluts);
});​

ajax POST ajax POST

function UbpdateSecondStage(arr) {

    var WebMethod ="GetSecondStageData";
    var page ="Default.aspx/";
    var target = page + WebMethod;
    var SendParameters = Sys.Serialization.JavaScriptSerializer.serialize(arr);
    jQueryAjxUpdt(target, SendParameters);

}


function jQueryAjxUpdt(targetUrl, SentPars) {
    $.ajax({
              type: 'POST',
              url: targetUrl,
              data: {
                     'sentobj':SentPars

              },
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (data) {
                                  //alert(data);
              }

   });
}

C# C#

  [WebMethod]
  public static string GetSecondStageData(object sentobj)
  {
      var x = sentobj;
    return ?? ...do i have to give a return.. even if i do not require one ?
  }

what is wrong with my code ?. 我的代码出了什么问题? it's first time i tried that approach . 这是我第一次尝试这种方法。 thanks. 谢谢。

I HAVE UPDATED FEW TIMES PLEASE READ IT AGAIN 我已经更新了几次,请再次阅读

在此输入图像描述

Modify your WebMethod like this and try again: 像这样修改您的WebMethod并再试一次:

[WebMethod]
public static string GetSecondStageData(object sentobj)
{
    var x = sentobj;
    return DateTime.Now.ToString();
}

First of all you should have specified what you are passing ( sending to the server) and what response you are getting( if you are luck ;)) back from the server. 首先,您应该从服务器返回指定您传递的内容(发送到服务器)以及您获得的响应(如果运气好的话)。 If it's throwing or rasing any error then that should be specified as well. 如果它正在抛出或发出任何错误,那么也应该指定。
Secondly there is no need to use the following code. 其次,不需要使用以下代码。

var WebMethod ="GetSecondStageData";
var page ="Default.aspx/";
var target = page + WebMethod;
var SendParameters = Sys.Serialization.JavaScriptSerializer.serialize(arr);

you can work like this as well 你也可以像这样工作

var test=new Object();
test.myArray=arr;
jQueryAjxUpdt("<Relative path to the directory such as '../home/default.aspx' >", JSON.stringify(test))

again change ajaxFunction as follows 再次更改ajaxFunction如下

function jQueryAjxUpdt(targetUrl, SentPars) {
$.ajax({
          type: 'POST',
          url: targetUrl,
          data: SentPars,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (data) {
                              //alert(data);
          }
    });
}

Haven't tested but I guess It will work let me know if don't. 没有测试,但我猜它会工作让我知道,如果没有。 ;-) ;-)
And finally I guess you haven't kept anything in your default.aspx page other then @page directive and It works even if you don't specify [WebMethod] attribute. 最后,我猜你没有在default.aspx页面中保留任何内容, @page指令,即使你没有指定[WebMethod]属性,它仍然有效。

Please try below code 请尝试下面的代码

JavaScript JavaScript的

function UbpdateSecondStage(arr) {
    var WebMethod ="GetSecondStageData";
    var page ="Default.aspx/";
    var target = page + WebMethod;
    var SendParameters =arr;
    jQueryAjxUpdt(target, SendParameters);

}

function jQueryAjxUpdt(targetUrl, SentPars) {
          $.ajax({
              type: "POST",
              url: targetUrl,
              data: '{"results": "' + SentPars + '"}',
              contentType: "application/json; charset=utf-8",
              processData: false,
              dataType: "json",
              success: function(msg) {
              //alert(msg);
                 here your code
              },
              error: function(x, e) {
                  if (x.status == 500) {
                      alert("An error has occurred during processing your request.");
                  }
              }
          });
      }

C# C#

[WebMethod]
public static string GetSecondStageData(object results)
{
    var x = results;
    return DateTime.Now.ToString();
}

Let me know result. 让我知道结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM