0

I am getting really stuck with an angular js HTTP post.

I have been using AngularJs in this particular project for nearly two years, and have never had this issue, it is also identical to other code, that is working in a lot of other places. I am lost, can anybody see anything I might be missing.

The server side code is running and working as I expect. One of my first thoughts was maybe I was not await-ing something, and it was returning something incomplete, but that doesn't seem to be true.

I ran the same post using Postman, and the response was 9503 which I am perfectly happy with, so I don't think the problem is anything on the server side.

I am adding some code snippets (debugs were added to try and see what was going on)

function in the controller:

    post() {
        debugger;
        this.loading++;
        this.service.post(this.functionImport)
            .then(r => this.postComplete(r),
                ex => {
                    debugger;
                    this.exception = ex
                })
            .finally(() => this.loading--);
    }

(I usually use promise.then().catcht() but I wanted to see if this made a difference)

The function in the service:

    post(model: FunctionImport): ng.IPromise<number> {
        const url = './api/FunctionImports/Import';
        return this.$http.post(url, model).then((r: ng.IHttpPromiseCallbackArg<number>) => {
            debugger;
            return r.data;
        });
    }

The exception that it catching on the promise

Object {data: null, status: -1, config: Object, statusText: "", headers: function}

Some headers in the post, from the Network tab in Chrome

Accept:application/json, text/plain, */*
Content-Type:application/json;charset=UTF-8
Origin:http://localhost:5001
Referer:http://localhost:5001/Admin.aspx?Page=FunctionImport
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36
X-Requested-With:XMLHttpRequest
JonathanPeel
  • 743
  • 1
  • 7
  • 19
  • could you explain why there is **.** (dot) in the url? I really didn't get that. – yugantar kumar Jun 10 '17 at 11:09
  • It is saying the URL is relative to the current page. When running somewhere else the application is in a Virtual Directory, so using just / will go to the wrong place. Also the current server side code is run, so I don't think there is a problem with the URL, and the same style works everywhere else. – JonathanPeel Jun 10 '17 at 11:12
  • I am not sure what u did in your other application, but it might be the **CORS** issue. Is the port to which you are requesting and from where you are requesting is different? If yes, then it is very much possible to be CORS issue – yugantar kumar Jun 10 '17 at 11:18
  • No. It is the same application – JonathanPeel Jun 10 '17 at 11:52
  • Going to check something... I think you might all get to laugh. – JonathanPeel Jun 10 '17 at 12:14
  • Yup, I was stupid, I left out `type="button"` I am not smashing my head on the desk. – JonathanPeel Jun 10 '17 at 12:19
  • Do I write an answer, incase it could help someone? Or do I delete this? – JonathanPeel Jun 10 '17 at 12:43
  • What type do you mean. I thought its a issue on API call, rather than invoking function via HTML – Saurabh Tiwari Jun 10 '17 at 16:08
  • The button called an angular function, but the I hadn't set the type of the button to 'button', so it was doing a submit. – JonathanPeel Jun 11 '17 at 11:51

0 Answers0