vendredi 11 août 2017

Retrieve null json data [object Object] from GET and POST method when consumed from Cordova

Retrieves null JSON data [object Object] from GET and POST method when consumed from Cordova

I'm developing a Web API system with many controllers, everything works perfectly from fiddler and The Web system which I developed, I hosted in IIS server and I can access all controllers from other computers as well, but when I consume it from Android and iOS devices on Cordova it gives null JSON data with error [object Object], and can't retrieve any single data.

This is my controllers:

[HttpGet]
[Route("api/Users")]
public IEnumerable<sp_users_Result> Users()
{
    using (var Entities = new System_Entities())
    {
        var Result = Entities.sp_users();
        return Result.ToList();
    }
}

[HttpPost]
[Route("api/Question")]
public HttpResponseMessage Question([FromBody] string txt)
{
    if (txt.Length == 0)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Can not leave any fields empty!");
    }
    else
    {
        try
        {
            using (var Entities = new System_Entities())
            {
                ObjectParameter Output = new ObjectParameter("txt_ID", typeof(int));
                Entities.sp_txt(Output, txt);
                var message = Request.CreateResponse(HttpStatusCode.Created, Output.Value);
                return message;
            }
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
        }
    }
}

This is from AJAX:

$.ajax({
            method: 'GET',
            url: 'http://ift.tt/2uuQKw3',
            contentType: 'application/json',
            dataType: 'jsonp',
            headers: {
                'Authorization': 'Bearer ' + sessionStorage.getItem('token')
            },
            success: function (data) {
               alert(data);
            },
            error: function (jgXHR) {
            alert(jgXHR);
            }
        });

I’m using JSONP for cross domain, and I already added the Cordova plugin whitelist, but still cannot ride on this problem. Cordova –v is : 7.0.1.

var jsonpFormatter = new JsonpMediaTypeFormatter(config.Formatters.JsonFormatter);
config.Formatters.Insert(0, jsonpFormatter);

Can someone help me with this?




Aucun commentaire:

Enregistrer un commentaire