我正在使用 Web API 2 + AngularJS 将数据返回到 Angular Controller ,但我不知道为什么我在 json 结果中不断收到此字符 ↵。
我的 ASP Controller 有这个方法。
public IHttpActionResult GetAditionalFieldCOnfiguration()
{
var ListAdditionalFields = new List<rgAdditionalField>()
{
new rgAdditionalField(){ Type="text", Label="Nombre", Model="first", Val="Carlo" },
new rgAdditionalField(){ Type="text", Label="Apellido", Model="last", Val="Carlos prueba"},
new rgAdditionalField(){ Type="text", Label="Direc", Model="direccion", Val="los alpes"},
};
return Ok(ListAdditionalFields);
}
我在我的 Angular Controller 中得到了这个(我尝试使用 $http、$resource 和 Restangular 并得到了相同的结果):
我在 WebApiConfig 中尝试了不同的配置,但没有任何效果,配置:
public static JsonSerializerSettings GetSettingListFormat(rgJsonSettingType type)
{
switch (type)
{
case rgJsonSettingType.List:
return new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.None, ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
case rgJsonSettingType.Single:
return new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
case rgJsonSettingType.SingleCamelCase:
return new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, ContractResolver = new CamelCasePropertyNamesContractResolver() };
case rgJsonSettingType.ListCamelCase:
return new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.None, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver() };
}
return new JsonSerializerSettings();
}
我尝试使用 HttpResponseMessage 它返回:
return Request.CreateResponse(HttpStatusCode.OK, ListAdditionalFields, JsonConfiguration.....)
没有别的。
你能帮我吗?
最佳答案
我不知道你的 AngularJS Controller 后面是什么,但我会用这种方式删除这个 ↵ 输入符号。
使用正则表达式在 AngularJS Controller 中创建一些函数来检查字符串是否包含 ↵,例如:
$scope.hasEnter = function(str) {
var enter= /\n/gi;
if (enter.test(str)) {
return true;
} else
return false;
};
如果这是真的,只需将其替换为您需要的字符即可。
希望这个想法对你有帮助。
关于asp.net-mvc - 从 Web api + AngularJS 中的 JSON 中删除 ↵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478204/