Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AJAX处理以空C#和JS形式出现的ID数组_Javascript_C#_Ajax - Fatal编程技术网

Javascript 使用AJAX处理以空C#和JS形式出现的ID数组

Javascript 使用AJAX处理以空C#和JS形式出现的ID数组,javascript,c#,ajax,Javascript,C#,Ajax,我试图从服务器端的字符串中获取数组,但我等待的ID是空的。请就以上提供的源代码提供建议。我不知道如何在CompareReturnAllResults(…)的范围内处理它们 函数compareFileResult(whlIDs){ var结果=0; $.ajax({ url:“/ajax/CompareReturnAllResults”, contentType:“应用程序/json;字符集=utf-8”, 键入:“GET”, 数据类型:“json”, 数据:{ID:whlIDs}, 成功:功能(

我试图从服务器端的字符串中获取数组,但我等待的ID是空的。请就以上提供的源代码提供建议。我不知道如何在CompareReturnAllResults(…)的范围内处理它们

函数compareFileResult(whlIDs){
var结果=0;
$.ajax({
url:“/ajax/CompareReturnAllResults”,
contentType:“应用程序/json;字符集=utf-8”,
键入:“GET”,
数据类型:“json”,
数据:{ID:whlIDs},
成功:功能(数据){
如果(数据!=null){
返回结果;
}
},
错误:函数(数据){
返回结果;
},
失败:功能(数据){
返回结果;
}
});
}
[HttpGet]
公共操作结果比较返回所有结果(字符串[]\u id)
{
列表结果=新列表();
foreach(变量id在_id中)
{
var whl=WHLConfig.Caches.WHLsCache.Where(w=>w.ID==ID.First();
var diffResponse=whl对象、whl方法、比较图(whl);
Common.Utilities.CompareFilesResult temp=新的Common.Utilities.CompareFilesResult(whl.ID,diffResponse.CountDeleted,diffResponse.CountInserted);
结果。添加(温度);
}  
string diff=string.Empty;
if(result!=null&&result.Count!=0)
{
返回Json(结果,JsonRequestBehavior.AllowGet);
}
其他的
{
返回Json(string.Empty,JsonRequestBehavior.AllowGet);
}
}

如果您使用了
contentType:“application/json;charset=utf-8”
,它会告诉服务器我们发送了一个
json
对象。为此,您必须使用
JSON.stringify()
方法

contentType
是您正在发送的数据类型,因此
application/json
;默认值为
application/x-www-form-urlencoded;字符集=UTF-8

JSON.stringify()
javascript
对象转换为
JSON
文本,并将其存储在字符串中

$.ajax({
    url: '/ajax/CompareReturnAllResults',
    contentType: "application/json; charset=utf-8",
    type: 'POST',
    dataType: "json",
    data: JSON.stringify({ _IDs: whlIDs }),
         ^^^^^^^^^^^^^^^^
    success: function (data) {
        if (data != null) {
            return result;
        }
    },
    error: function (data) {
        return result;
    },
    fail: function (data) {
        return result;
    }
});
C#

C#

公共操作结果比较返回所有结果(列表ID)
{
//代码
}

我想我需要将“传统”设置为true

脚本:-

jQuery.ajaxSettings.traditional = true
第一个:-

 $.ajax({
       url: 'controller/GetArray',
       data: JSON.stringify({
          employee: arrEmployee
       }),
       success: function(data) { /* Whatever */ }
    });


****Second One:-****

$.ajax({
     type: "POST",
     url: "controller/GetArray",
     data: {employee: arrEmployee},
     success: function (data) { /* Whatever */  }
});
型号:-

public class Employee
{
  public int Id;
  public string Name;
}
在控制器中:-

public ActionResult GetArray(Employee[] employee)
{
    // code here what u do
}

希望它的工作

我试过了,但还是一样的,ID再次显示为空。我不确定我是否在public ActionResult CompareReturnAllResults(string[]\u ID)的范围内正确处理了它们。尝试使用
字符串ID
,然后使用
JavascriptSerializer
来获取数组。另一种方法是使用
列表ID
。此外,名称必须匹配。试试这个:
JSON.stringify({u-ID:whlIDs})
而不是
JSON.stringify({ID:whlIDs})
请,写下它作为我如何在这里使用JavascriptSerializer的示例。非常感谢。您已经声明了
string[]\u id
,因此请确保在ajax调用中使用相同的ID<代码>数据:{u id:whlIDs},不幸的是,浏览器中的ID再次以字符串数组的形式出现,但在服务器端,它们在列表中为空。它正在使用Alexandru Ionut Mihai给出的示例。也谢谢你!
jQuery.ajaxSettings.traditional = true
 $.ajax({
       url: 'controller/GetArray',
       data: JSON.stringify({
          employee: arrEmployee
       }),
       success: function(data) { /* Whatever */ }
    });


****Second One:-****

$.ajax({
     type: "POST",
     url: "controller/GetArray",
     data: {employee: arrEmployee},
     success: function (data) { /* Whatever */  }
});
public class Employee
{
  public int Id;
  public string Name;
}
public ActionResult GetArray(Employee[] employee)
{
    // code here what u do
}