我从Angular应用程序调用自定义Web API,我需要两次JSON.parse()我的响应才能访问属性。我不确定为什么会这样。
/// <summary>
/// Gets list of printers
/// </summary>
[HttpGet]
public IHttpActionResult GetPrinterList()
{
try
{
List<Printer> pl = new List<Printer>();
// List the print server's queues
PrintQueueCollection myPrintQueues = new PrintServer(@"\\LPH-Printers").GetPrintQueues();
foreach (PrintQueue pq in myPrintQueues)
{
Printer p = new Printer();
p.Name = pq.FullName;
pl.Add(p);
}
return Ok(JsonConvert.SerializeObject(pl));
}
catch (Exception e)
{
return BadRequest(e.ToString());
}
}
这是我的API中的方法,下面是我在Angular中调用它的方法
'use strict';
app.factory('printerService', ['$http', 'ngAuthSettings', function ($http, ngAuthSettings) {
var serviceBase = ngAuthSettings.apiServiceBaseUri;
var printerServiceFactory = {};
var _DefaultPrinter = function (val) {
return $http.get(serviceBase + 'api/LibertyMobile/GetUserDefaultPrinter', {
params: { 'username': val }
})
};
var _SetDefaultPrinter = function (userName, DefaultPrinter) {
return $http({
url: serviceBase + "api/LibertyMobile/SaveUserDefaultPrinter",
method: "POST",
params: { 'username': userName, 'printer': DefaultPrinter }
});
}
var _GetPrinterList = function () {
return $http.get(serviceBase + 'api/LibertyMobile/GetPrinterList');
}
printerServiceFactory.DefaultPrinter = _DefaultPrinter;
printerServiceFactory.SetDefaultPrinter = _SetDefaultPrinter;
printerServiceFactory.GetPrinterList = _GetPrinterList;
return printerServiceFactory;
}]);
非常感谢任何帮助。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
//config.Routes.MapHttpRoute(
// name: "GetPartNumbers",
// routeTemplate: "api/Inventory/GetPartNumbers/{partnum}/{user}",
// defaults: new { controller = "Inventory" }
//);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { controller = "Inventory", action = RouteParameter.Optional }
);
}
}
以上是我的WebApiConfig.cs代码。
答案 0 :(得分:0)
此
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="75dp"
>
<ImageView
android:id="@+id/contactphone"
android:layout_width="75dp"
android:src="@drawable/phonecall"
android:layout_margin="10dp"
android:background="@drawable/contact_icon_round"
android:layout_height="75dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="75dp"
>
<ImageView
android:id="@+id/contactemail"
android:layout_width="75dp"
android:src="@drawable/mail"
android:layout_margin="10dp"
android:background="@drawable/contact_icon_round"
android:layout_height="75dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="75dp"
>
<ImageView
android:id="@+id/contactlocation"
android:layout_width="75dp"
android:src="@drawable/location"
android:layout_margin="10dp"
android:background="@drawable/contact_icon_round"
android:layout_height="75dp" />
</LinearLayout>
</LinearLayout>
框架将序列化为您传递的值,但您也在使用return Ok(JsonConvert.SerializeObject(pl));
对其进行序列化,然后将其传递给操作结果,从而进行双序列化。
只需将值传回
JsonConvert.SerializeObject
让框架做到这一点。