1

I am new to angular and ionic, so I started creating simple ionic application with AngularJS but I am facing a problem, I tried searching it on google but could not figure it out.

for testing I hard-corded parameters in angular controller. after click on submit button from ionic app, it hitting to the relevant action in API controller but data showing as null,

if anyone can help to figure this out it will be appreciate,

AngularJs Controller

 $scope.savenewdansal= function(newDansal){

      $http({
          method  : 'POST',
          url     : 'http://localhost:51079/api/Dansal/SaveNewDansal',
          dataType: 'json',
          data: JSON.stringify({"MainCategory": "category1","SubCategory": "subCateMy","District": "Central","City": "Colo","date": "2000-01-01T00:00:00","Time": "2001-01-01T00:00:00","Venue": "kurudnwa","contacts": "nocon"}),
          headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
     })
      .success(function(data) {
        if (data.errors) {
            console.log(data.errors.name)
        } else {
          console.log(data.message);
        }
      });

WebAPI Action

[HttpPost]
    [ActionName("SaveNewDansal")]
    public VM_NewDansal SaveDansal(VM_NewDansal newDansal)
    {

        return newDansal;
    }

Model Class

 public class VM_NewDansal
{
    public string MainCategory { get; set; }
    public string SubCategory { get; set; }
    public string District { get; set; }
    public string City { get; set; }

    public string date { get; set; }
    public string Time { get; set; }
    public string Venue { get; set; }
    public string contacts { get; set; }

}

both Ionic App and WebAPI Service running on localhost Thank you

7
  • Just pass an object instead of stringifying. And why is this header necessary? Commented May 12, 2016 at 6:12
  • I saw it in another post that is why created it in that way, but if I moved that, app giving me this error "Response for preflight has invalid HTTP status code 405" Commented May 12, 2016 at 6:18
  • What did you try? Did you remove the stringify? Commented May 12, 2016 at 6:24
  • yes i removed and try but still no clue, I got the posting json data set from Network tab of browser, if I call method with same data in POSTMAN, it is working. Commented May 12, 2016 at 6:28
  • Just do $http.post(uri, data) and callbacks Commented May 12, 2016 at 6:32

1 Answer 1

1

After searching through some another post from google, i find out a solutions for my problem, thanks for everyone who tried to help me.

first I change

headers : {'Content-Type': 'application/x-www-form-urlencoded'}

to

headers : {'Content-Type': 'application/json'} 

then it gave me following error,

XMLHttpRequest cannot load http://localhost:51079/... Response for preflight has invalid HTTP status code 405

so figure it out that error occurred because it a Cross-Origin need to enable because my ionic app and webserice both run in local host insame machine but in diffrent ports.

so if anyone want to enable CORS check follow link

link

otherwise check it in Internet Explorer,it does not consider the port when comparing origins.

thanks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.