I am new to angular, and I'm trying to make a call to a Rest API and get its response. My issue is that my JavaScript keeps getting stuck on the Ajax call. I'm not sure if it's the data I am sending or the syntax of the Ajax call. I tried to alert 'Hello world' and that worked, then I alerted the JSON array and that was formatted correctly, but when I do the Ajax post, I don't get any response at all.
Any insight would be nice, thank you.
test.html
<button onclick="myFunction()">Post it</button>
test.js
function myFunction() {
var postData = [{"logintype":"1","user":"Administrator","password":"12345","controlid":"999","host":"192.168.2.164"}
];
$.ajax({
url: '192.168.2.164/isapi/rip.dll/rest/session',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify( postData ),
success: function(){
alert('hello');
},
error: function(){
alert('error');
}
});
};
I am new to angular, and I'm trying to make a call to a Rest API and get its response. My issue is that my JavaScript keeps getting stuck on the Ajax call. I'm not sure if it's the data I am sending or the syntax of the Ajax call. I tried to alert 'Hello world' and that worked, then I alerted the JSON array and that was formatted correctly, but when I do the Ajax post, I don't get any response at all.
Any insight would be nice, thank you.
test.html
<button onclick="myFunction()">Post it</button>
test.js
function myFunction() {
var postData = [{"logintype":"1","user":"Administrator","password":"12345","controlid":"999","host":"192.168.2.164"}
];
$.ajax({
url: '192.168.2.164/isapi/rip.dll/rest/session',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify( postData ),
success: function(){
alert('hello');
},
error: function(){
alert('error');
}
});
};
Share
Improve this question
edited Feb 22, 2019 at 2:22
Lauren Van Sloun
1,2655 gold badges20 silver badges29 bronze badges
asked Sep 3, 2015 at 13:44
Sari RahalSari Rahal
1,9553 gold badges34 silver badges55 bronze badges
6
- stop working on the app and start reading some documentation ... your example doesn't have anything to do with angular. – sirrocco Commented Sep 3, 2015 at 13:59
- Are you using angularjs then use docs.angularjs/api/ng/service/$http service – ARIF MAHMUD RANA Commented Sep 3, 2015 at 14:01
- Define "Getting stuck". Does the success function fire? Does the error function fire? Do you see messages in the Developer Tools Console? Does you see the request in the Developer Tools Net tab? Do you see a the response there? Are both formatted as you would expect? – Quentin Commented Sep 3, 2015 at 14:14
- What does this have to do with angular-seed? – Quentin Commented Sep 3, 2015 at 14:15
- Sorry guy, I made my question more specific. – Sari Rahal Commented Sep 3, 2015 at 14:23
2 Answers
Reset to default 4You have specified a relative URL, where I think you intended to specify an absolute URL. If the current page URL is http://localhost/myapp/
, and you request 192.168.2.164/isapi/rip.dll/rest/session
, that URL is resolved as http://localhost/myapp/192.168.2.164/isapi/rip.dll/rest/session
.
If 192.168.2.164
is the ip address of the server you are trying to hit (and not a directory relative to your current path on your server), you will need to add //
to the beginning of the URL to make it absolute (well, schema-relative at least):
$.ajax({
url: '//192.168.2.164/isapi/rip.dll/rest/session',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify( postData ),
success: function(){
alert('hello');
},
error: function(){
alert('error');
}
});
Your issue has nothing to do with angular. What I will refer you to is the angular docs description of how to do a POST request and a small example of the syntax taken from the docs.
Learn to use $http or something similar if you want to develop with angular. https://docs.angularjs/api/ng/service/$http
Small example:
// Simple POST request example (passing data) :
$http.post('/someUrl', {msg:'hello word!'}).
then(function(response) {
// this callback will be called asynchronously
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});