Invaild Json Result through Web Api in angular js - c#

I have build a http post web api in asp which return the following string in Json
RootObject rootObject = new RootObject()
{
status = "User Registered"
};
msg = JsonConvert.SerializeObject(rootObject);
Below is my angular js controller in which I am consuming that web api
.controller('signupCtrl', function($scope,$http,$ionicPopup,$state,$ionicHistory) {
$scope.signup=function(data){
var link = 'http://xxxxxxxxxxxxxxxxxxxxxx/api/Home/RegisterUser';
//using http post
//passing values to parameter
$http.post(link, {RegisterName : data.name, RegisterUserName : data.username, RegisterPassword : data.password , RegisterEmail: data.mail , RegisterMobile : data.mobile})
.then(function (res){ //if a response is recieved from the server.
$scope.response = res; //contains Register Result
console.log($scope.response);
});
}
})
With the above code I am getting following result in google chrome console
I am try to get that status only to match it value but I am unable to do so.
The doubt I am having is that json format

console.log(JSON.stringify($scope.response)) will do what you need.
If you're wanting those particular value, you can just access those and pass them to log.
console.log($scope.response.data['status']);

you get the json as :
$scope.response = res.data;
might be you require JSON.parse(res.data) or $.parseJSON(res.data) for getting json object

Related

Is it possible to call an Dynamics CRM action with a JSON request through a WebApi with IOrganizationService?

TL;DR:
I am calling a WebApi, the WebApi authenticates against the CRM and use the IOrganizationService, so my request is a JObject and not an Entity or EntityReference, it gives me this error:
Error: Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.
Context:
I built a web application in angular and I built a WebApi so I can call some custom actions in CRM:
Angular APP | WebApi | OnPremise CRM
So, when I call the WebApi, there is a controller that turns my request into a OrganizationRequest:
Request for WebApi:
{
"ActionName": "custom_actionname",
"Parameters":
[
{
"Key": "EntityInputParameter1",
"Value": {"#odata.type":"Microsoft.Dynamics.CRM.any_entity"}
}
]
}
I read this request on my WebApi and turn that into a request for CRM
Request for CRM:
OrganizationRequest request = new OrganizationRequest("custom_actionname");
request.Parameters["EntityInputParameter1"] = {"#odata.type":"Microsoft.Dynamics.CRM.any_entity"} // This is a JObject
OrganizationResponse response = service.Execute(request);
When I make the request, it gives me the following error:
Error: Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.
If I make the request directly to the action it works, but I cannot do that due security policies.
One option could be turn the request into a valid CRM request (parsing {"#odata.type":"Microsoft.Dynamics.CRM.any_entity} into a Entity type) but CRM has a lot of parsing escenarios and could be very complex.
Another option could be sending the request through web and stop using the IOrganizationService but I cannot change that.
I am making this question so anybody that has this error can find the "solution" because I searched a lot and nobody refers this behavior directly.
I am probably turning my InputEntityParameter into string, and I will send the JSON, so I can parse the JSON on my action, but I was looking if anybody else had this error or another approach.
I tested it on one of my Dev Environment with Entity as Parameter.
Below is the code I used in console application to fire Action with Entity as parameter. It ran successfully
var request = new OrganizationRequest("new_test");
//request.Parameters.Add("Target", xAccountReference);
request.Parameters.Add("Param2", "abc");
request.Parameters.Add("Param1", new Entity("account",Guid.Parse("2fe32f22-d01d-ea11-80fa-005056936c69")));
Service.Execute(request);
Below is the Javascript code which used CRM Webapi to execute Action with Parameter. Ignore the XRM.Webapi command but interesting for you would be passing parameters in webapi.
var parameters = {};
parameters.Param2 = "abcd";
var param1 = {};
param1.accountid = "2fe32f22-d01d-ea11-80fa-005056936c69"; //Delete if creating new record
param1["#odata.type"] = "Microsoft.Dynamics.CRM.account";
parameters.Param1 = param1;
var new_testRequest = {
Param2: parameters.Param2,
Param1: parameters.Param1,
getMetadata: function() {
return {
boundParameter: null,
parameterTypes: {
"Param2": {
"typeName": "Edm.String",
"structuralProperty": 1
},
"Param1": {
"typeName": "mscrm.account",
"structuralProperty": 5
}
},
operationType: 0,
operationName: "new_test"
};
}
};
Xrm.WebApi.online.execute(new_testRequest).then(
function success(result) {
if (result.ok) {
//Success - No Return Data - Do Something
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
I can confirm that you are mixing Webapi and orgservice call. You can definitely call Action from Webapi of Dynamics. I just used Postman to call Action and I was successful. Blog reference to use Postman for CRM webapi
Below Body as json in Postman and I get Action to run.
{
"Param1":"string test",
"Param2":{
"accountid":"b6b35fd0-b9c3-e311-88e2-00505693000c",
"#odata.type":"Microsoft.Dynamics.CRM.account"
}
}

i want to create a simple dotnet(vb.net or c#.net) class

I have a small task. I need the a .net class library created that communicates with a RPC REST service. All I need is the basic framework. So if you can just get it to pass the API token and get back the login credential
https://simplybook.me/en/api/developer-api/tab/explorer_api
The have a sample API Key I think you can use:
company: mib
API key: f43618e37b82004066d60db3431f4a06392599a6cfcafa8268bf25becc0ec7d7
You can use JSONRPC Client as shown below Link
using (Client rpcClient = new Client("someURL"))
{
rpcClient.Headers.Add("X-Application", "MyApplicationKey");
Request request = rpcClient.NewRequest("SportsAPING/v1.0/listMarketBook");
GenericResponse response = rpcClient.Rpc(request);
if (response.Result != null)
{
JToken result = response.Result;
}
else
Console.WriteLine(string.Format("Error in response, code:{0} message:{1}",
response.Error.Code, response.Error.Message);
// Example with positional parameters
JArray parameters = JArray.Parse(#"['Small', 'Medium', 'Large' ]");
Request resuestWithPostionalParameters = rpcClient.NewRequest("SportsAPING/v1.0/listMarketBook", parameters);
// Example with named parameters
JObject namedParameters = JObject.Parse(#"{ CPU: 'Intel', }");
Request resuestWithNamedParameters = rpcClient.NewRequest("SportsAPING/v1.0/listMarketBook", namedParameters);
}

Failed to serialize the response body after upgrading from ODATA v3 to v4

I have a Web API server where I use ODATA (and EF 6) to return a list of Items (consumed by a WinForms client that uses a DevExpress ODataInstantFeedbackSource bound to their GridControl).
Here is the Web API controller method that returns the list of Items:
public IHttpActionResult GetItems(ODataQueryOptions<Item> queryOptions)
{
var customerNumber = Request.Headers.GetValues("CustomerNumber").FirstOrDefault();
try
{
queryOptions.Validate(_validationSettings);
var query = queryOptions.ApplyTo(Context.Items) as IQueryable<Item>;
var items = query.AsEnumerable().Select(i => new Item()
{
ItemNumber = i.ItemNumber,
ItemDescription = i.ItemDescription,
<snip>
RebateAmount = RebateUtil.CalculateInstantRebates(i.ItemNo, customerNumber),
}).AsQueryable();
return Ok(items);
}
catch (ODataException ex)
{
return BadRequest(ex.Message);
}
}
Before I upgraded to ODATA v3, the above was working perfectly; after upgrading, I now get:
The ObjectContent1 type failed to serialize the response body for
content type "text/plain; charset=utf-8", The value of type
"System.Linq.EnumerableQuery1[[AcmeService.Model.Item,
AcmeService.Model, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]" could not be converted to a raw string.
I see the above message in the HTTP response body (Status 500). The request URL is: http://acme.us/odata/Items/$count
Note that if I make the GetItems controller simply do a "return Ok(Context.Items)", it works fine (no error, but I need to be able to set the RebateAmount value before returning the data).
What am I doing wrong?
I had the same error message in an another context (without devexpress), but maybe this could help you.
I solved it by replacing this namespace :
using System.Web.Http.OData;
by :
using System.Web.OData;

RESTful Swift json parsing problems

I created a RESTful api in Visual Studio with the following Controller:
[RoutePrefix("api/json")]
public class JsonController : ApiController
{
[Route("person")]
public string GetPerson()
{
Person person = new Person(0, "John Doe", 99);
JavaScriptSerializer serialize = new JavaScriptSerializer();
return serialize.Serialize(person);
}
}
When I navigate to the api through the browser I get this result:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">{"Id":0,"Name":"John Doe","Age":99}</string>
In my Swift code I´m trying to get this result and parse it to my textboxes with the following code:
var url = "THE URL TO MY SITE"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
if (jsonResult != nil) {
println(jsonResult)
// process jsonResult
//self.txtStringiFiedText.text = jsonResult["Name"] as NSString
} else {
println(error)
}
})
When I´m running this I get this error:
0x0000000000000000
But when I for example test this API:
http://jsonplaceholder.typicode.com/posts/1 it works and I get the data provided in the JSON.
So there has to be something wrong with my RESTful API Controller method. Anyone has an idea of what it could be?
Appreciate help.
Rather than returning JSON, your API is returning a string that has been serialized as XML. You need to
Just return a Person -- it get serialized for you.
Make sure a JSON formatter is configured: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization. (It should be by default.)

SignalR, passing an object from the HUB to the Client (MVC C#)

I've looked around at other questions but they don't seem to fully answer this question, I'm trying to pass an object via JSON to the client Javascript. I'm using Newtonsoft.Json to make the process easier, but I can't seem to recieve the object.
Here's the code:
When a connection is made, I call the Hub using start().done() in the client javascript:
//start comm with server
$.connection.hub.start().done(function () {
console.log('Grabbing playlist data');
Playlist.server.requestPlaylist();
});
This calls the following method, which is supposed to grab the object and pass it back:
public void requestPlaylist()
{
var playlistData = (from c in db.Playlist where c.ID > 0 select c).Include(h => h.Song).ToList();
Playlist player = new Playlist();
foreach (var item in playlistData)
{
player.ID = item.ID;
player.Profile = item.Profile;
player.Song.ID = item.Song.ID;
player.Song.name = item.Song.name;
player.upvotes = item.upvotes;
}
string jsonObject = JsonConvert.SerializeObject(player);
Clients.All.recievePlaylist(jsonObject);
}
SO here, I'm searching the database, getting the results and storing it into the playlist model, then using newtonsoft.json to convert the model into a json object (Its roughly the same principle they have as an example on their site).
The client javascript that is invoked from this is:
function recievePlaylist(jsonObject) {
console.log('test to recieve data: ' + jsonObject.ID + ' test.');
};
Now just for testing purposes I'm just logging out out to the console, but this come back with nothing:
"test to recieve data: test." is how it comes back.
What am I missing?
Because you convert the object to a string on the server before passing it to the client, the client receives a string. Your string representation of a json object doesnt have an ID property so the value will be "undefined".
On the client you can use this to convert the string to a json object:
jsonObject = JSON.parse(jsonObject);
Just add that line to the top of your recievePlaylist function.
Note: You shouldn't actually need to convert your server object to a json string on the server side. SignalR automatically converts your server side objects to json objects on the client.
If you call WebAPI and receive json response/result on client side (JavaScript/jQuery). The way is general for both SignalR or WebAPI in regards of parse jsone response and the use it as object.
var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" );

Categories