0

I want to post some data to API Controller and receive a response like failure or Accept my controller is

public HttpResponseMessage Post([FromBody]Person person)
{
    return new HttpResponseMessage(HttpStatusCode.OK);
}

and my front end code to send the Person data to My API is

function myfunc() {

    $(function () {
        var person = {'id':"1",'Name':"Mohammad",FamilyName:"Basiri"};
        //preventDefault();
        $.ajax({
            type: "POST",

            //contentType: "application/json",

            data: (person),

            url: "http://localhost:40027/api/Person",
            dataType: 'json',
            //contentType: "application/json",
            success: function (d) {
                alert("Done");
            },
            error: function (result) {
                var e = JSON.stringify(result);
                alert(e);
            }
        });
    });

and the status code received is 200 OK but JQuery Function fires error if the content type which I commented be used then Person can not be sent my question is what can I do to send a response to JQuery that it receives it correct and doesn't fire error ?

thanks in advance

Komal12
  • 3,340
  • 4
  • 16
  • 25
M.h Basiri
  • 56
  • 9
  • I think your problem is you are sending back an HttpResponse not a Json object. I'm not sure what framework your using but you need to send whatever repsents a json object. – FernandoZ Apr 19 '17 at 07:24
  • thanks for you response but i tried that and that was the same and jquery fired error – M.h Basiri Apr 19 '17 at 07:28
  • I think this may shed some light on the issue http://stackoverflow.com/questions/18701282/what-is-content-type-and-datatype-in-an-ajax-request – FernandoZ Apr 19 '17 at 07:31
  • what ever I have done didn't work when ever I add content type the ajax method doesn't even reach my Controller – M.h Basiri Apr 19 '17 at 07:36
  • Seems like this works... maybe it will help http://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-object – FernandoZ Apr 19 '17 at 07:36
  • Notice that in the sample the full URL is not specified...only api/Person – FernandoZ Apr 19 '17 at 07:41
  • Also notice in the example that person should be a Javascript object (no quotes for properties names... and then uses json stringify to send – FernandoZ Apr 19 '17 at 07:43
  • And no need to set the datatype...just the content type – FernandoZ Apr 19 '17 at 07:44
  • I can reach to my controller in case I Delete "ContentType=[what ever] " but my problem is what ever I return in API Controller including Json ,Http Response ,string or boolean the Jquey fires error – M.h Basiri Apr 19 '17 at 07:46
  • Did you try returning Request.CreateReaponse to return your value as in the example from the last link? – FernandoZ Apr 19 '17 at 08:05
  • yes I tried and this error message comes : {"readyState":0,"status":0,"statusText":"error"} – M.h Basiri Apr 19 '17 at 08:13

3 Answers3

0

$(function () { var person = {id: "1", Name: "Mohammad", FamilyName: "Basiri"}; $.ajax({ type: "POST", data :JSON.stringify(person), url: "api/Person", contentType: "application/json"}); });

Taken from example and modified...try that first and then add your success and error methods back in

FernandoZ
  • 439
  • 6
  • 13
  • problem is not the FromBody I receive in controller, It's the Response controller sends for JQuery Ajax , the JQuery cannot interpret the response and thats why it fires Error – M.h Basiri Apr 19 '17 at 08:44
0

I didn't get you exactly what are you trying to say but try below approach it may help you.

--Thank You

       $.ajax({
        type: "POST",
        headers:{ contentType: 'application/json' },        
        data: {person:person},
        url: 'http://localhost:40027/api/Person',
        cache:false,      
        success: function (d) {
            alert("Done");
        },
        error: function (jqXHR) {
            alert(jqXHR.statusText);
        }
    });
0

1st Way

if you are using HttpResponseMessage(HttpStatusCode.OK) then in 
ajax dataType:'text' should be as

Second Way

You can use HttpResponseMessage(HttpStatusCode.Created) then in 
 ajax dataType:'text' should be as

Example of ajax

     $.ajax({
               type: 'POST',
               url: baseUrl + 'api/CompanyInfo',  //path of api
               contentType: "application/json", // send to server, json data
               dataType: "text", //receive from server
               async: true, //
               traditional: true,
               data: JSON.stringify(json), //contentType is json, so i have converted data into json
               success: function (data, status, jqXHR) {
                   if (jqXHR.statusText = 'OK' && jqXHR.status == 200) {
                       ShowMessage("Update Successfully", 'Success');
                   }
               },
               error: function (XMLHttpRequest, textStatus, errorThrown) {
                   ShowMessage(XMLHttpRequest.responseText, 'Error');
               }
           });
Asif Raza
  • 971
  • 6
  • 14