$http angularjs shows error on callback - javascript

I tested my php alone and it gave me this result
{"tabId":1,"tabName":"Main","uId":"1"}{"tabId":2,"tabName":"Photography","uId":"1"}
but my angularjs can't receive the callback, it return an error somewhere in angularjs
userId = '1';
$http({
url: "php/loadTab.php",
method: "GET",
params: {'userId':userId}
}).success(function(data, status, headers, config) {
console.log(data);
}).error(function(data, status, headers, config) {
});
the wierd thing I reuse the exact ajax code and it worked previously. Any thought on this?
the error : SyntaxError: Unexpected token {
at Object.parse (native)
the more strange thing : I purposely add another echo on my php and it able to console the value. what?!

The data from the server isn't valid JSON.
$httpProvider.defaults.transformResponse will try to parse the JSON if the data looks like JSON.
https://docs.angularjs.org/api/ng/service/$http
To fix this you could make the parent an array of objects like this
[{"tabId":1,"tabName":"Main","uId":"1"},{"tabId":2,"tabName":"Photography","uId":"1"}]

Related

jQuery or AngularJS Ajax "POST" request to server using code below however do not sending param data to server.

"POST" request to server using code below
however do not sending param data to server.
I tried jQuery Way and
var request = $.ajax({
url: baseUrl,
type:'post',
data: 'userId=userabcd&passwd=abcd1234',
});
request.done(function( msg ) {
console.log(msg);
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
AngularJS Way.
$http({
method : 'POST',
url : baseUrl,
headers : { 'Content-Type': 'application/x-www-form-urlencoded' },
data : 'userId=userabcd&passwd=abcd1234'
}).success(function(data, status, headers, config) {
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
}).error(function(data, status, headers, config) {
console.log(data);
// called asynchronously if an error occurs
// or server returns response with an error status.
});
but both are not sending "POST" query to server.
what am i doing wrong
please help.
server information
Mircrosoft/IIS8.0, ASP
Without seeing your server-side code it is difficult to say if that is a problem. The JS you have presented generally looks okay. You say that it runs without errors but fails to produce the expected results on the server. So I suggest you add the server-side code.
Both cases accept data as an object rather that a string which is generally more convenient. (avoids having to deal with encoding characters yourself)
For example:
data: {userId:'userabcd', passwd:'abcd1234'},
Have a read of the the documentation for $.ajax and $http

Flask - Getting POST data from Angular $http.post()

In a very basic test web app I am making, I am using angular to run a function when a form is submitted. The function asynchronously posts data to a simple api I built which is supposed to input data into a database dependent on the POST information it receives. It seems like the POST is working correctly on the front end, however I cannot access request.json from Flask at all or get any of the post data. I feel like this problem may be something simple I have overlooked but as of now I cannot figure it out at all. Here is some of the code:
AngularJS:
$scope.submitAddMsg = function (){
var data = {'author': $scope.msgauthor, 'message': $scope.msgmsg};
$http.post('/addMessage', data, {headers: {'Content-Type': 'application/json'}}).
success(function(data, status, headers, config) {
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
}).
error(function(data, status, headers, config) {
alert(JSON.parse(data));
});
};
Flask view function for /addMessage
#app.route('/addMessage', methods=['POST'])
def addMessage():
#results in 'None type not iterable'
#response = request.form.get('author')
#results in a 400 error
#response = request.get_json()
#results in a 400 error
#response = request.get_json()
#results in 'author' not defined
#name = request.args.get('author')
#return jsonify(author = author)
return str(jsonify(response))
I cannot stop getting errors as if the request is not what I think it should be, is there something else I should be doing to properly handle this? Because I cannot access any POST information when using Angular to send the POST or even a REST Client with payload exactly how the angular is sending data.
Here is the JavaScript console to see what data, status, headers, and config ends up being in the success function that runs after the POST:
<Response 46 bytes [200 OK]>
testbase.js:53 200
testbase.js:54 function (c){a||(a=Xc(b));return c?(c=a[z(c)],void 0===c&& (c=null),c):a}
testbase.js:55 Object {method: "POST", transformRequest: Array[1], transformResponse: Array[1], headers: Object, url: "/addMessage"…}data: Objectheaders: ObjectAccept: "application/json, text/plain, */*"Content-Type: "application/json"__proto__: Objectmethod: "POST"transformRequest: Array[1]transformResponse: Array[1]url: "/addMessage"__proto__: Object
Any help on getting this working right is much appreciated, let me know if you need more information
you can use request.data to get the raw post data.
you can also set the silent Flag of get_json to True so you can get the exact message of failure.
from the docs
get_json(force=False, silent=False, cache=True)
Parameters:
force – if set to True the mimetype is ignored.
silent – if set to False this method will fail silently and return False.
cache – if set to True the parsed JSON data is remembered on the request.
Try adding app.debug = True before you start the app and try again.
You can also try:
message = request.json.get('message')
And I would also try putting this in your route
['POST', 'OPTIONS'])

SOAP fault error when using AngularJS to request from SSRS

I'm using AngularJS to try to pull down a list of reports from SSRS to display in an iframe. The problem I'm running into is that I am getting a SOAP fault error when doing the POST request.
Here is what the Angular controller looks like that is making the POST.
function ReportSSRSController($scope, $http, $location) {
$scope.request = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">'
+ '<soap:Body>'
+ '<m:ListChildren xmlns:m="http://example.com/ReportingServer/ReportService2010">'
+ '<m:ItemPath>/reports</m:ItemPath>'
+ '<m:Recursive>false</m:Recursive>'
+ '</m:ListChildren>'
+ '</soap:Body>'
+ '</soap:Envelope>';
$http({
method: 'POST',
url: '/ReportServer/ReportService2010.asmx',
data: $scope.request,
headers: {
'Content-Type': 'application/soap+xml; action="http://schemas.microsoft.com/sqlserver/reporting/2010/03/01/ReportServer/ListChildren"'
}
})
.success(function(data, status, headers, config){
console.log('In Success');
$scope.data = data;
})
.error(function(data, status, headers, config){
console.log('In Error');
console.log(data);
console.log(config);
});
}
And here is the gist of the SOAP fault error.
System.Web.Services.Protocols.SoapException: The value for parameter 'ItemPath' is not specified. It is either missing from the function call, or it is set to null.
As you can see in the Angular code, the ItemPath is included in the SOAP body in the same namespace as the function call. I can also see it in the console as the output of the data variable in the error block. So I'm wondering why it is not able to find that information.
Is there something I'm missing maybe in the way Angular is handling the POST request? Or have I not formulated the SOAP request correctly?
It turns out that the issue is related to the namespace in the SOAP XML.
When I changed the namespace to
http://schemas.microsoft.com/sqlserver/reporting/2010/03/01/ReportServer
to match the SOAP action (minus the command at the end); the request returned a valid SOAP response.
I'm not sure if this is a limitation of SOAP or SSRS though since I'm not very familiar with either.

How to use Jquery $.Ajax in angularjs

I am trying to use normal jQuery Ajax in anuglarjs .
$.ajax({
type: "POST",
url: window.apirooturl + "/logger",
contentType: "application/json",
data: angular.toJson({
url: $window.location.href,
message: errorMessage,
type: "exception",
stackTrace: stackTrace,
cause: ( cause || "")
})
});
But i am getting an error
Below two messages appears in the firbug
$ is not defined
Error: [$rootScope:inprog] $digest already in progress
Do we need to configure something to use normal jquery function in anagularjs?
I tried to look on Google but could not get a answer
More information :
I am trying to send client side error to the server side and trying to implement a global exception handler as suggested in this post .
http://www.bennadel.com/blog/2542-logging-client-side-errors-with-angularjs-and-stacktrace-js.htm
You are not supposed to use $.ajax in angular you need to use $http request
for example, this is ho you should use post:
$http({
url: "http://example.com",
method: "POST",
data: {"a":"b"}
}).success(function(data, status, headers, config) {
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
please see more at : https://docs.angularjs.org/api/ng/service/$http
1) If you reference full jQuery library, Angular should use it instead of it's own. Do you reference it before angular.js file?
2) Wrap your code with $timeout to prevent error '$digest already in progress'

Unable to get authenticated using Redmine REST API

I am building a mobile application for Redmine. So, to get data from the Redmine server, I need to authenticate the user by passing his password and username. In Redmine API documentation, two methods are mentioned for authentication.
using your regular login/password via HTTP Basic authentication.
using your API key which is a handy way to avoid putting a password
in a script. The API key may be attached to each request in one of
the following way:
- passed in as a "key" parameter
- passed in as a username with a random password via HTTP Basic
authentication
- passed in as a "X-Redmine-API-Key" HTTP header (added in Redmine
1.1.0)
I tried to send a get request as follows, I can see the issue list in response to the Get request in browser. But the success callback is not triggering.
var url= "http://username:password#redmine.qburst.com/issues.json";
$http({
method: 'JSONP',
url: url
}).
success(function(data, status) {
console.log(data);
}).
error(function(data, status) {
console.log("Error "+status);
});
The following error is coming in console.
SyntaxError: missing ; before statement
{"issues":[{"id":139989,"project":{"id":215,"name":"Book Meeting Room
Error status is 0.
Different to standard JSON, JSONP responses should be wrapped in a function call.
Instead of:
{"issues":[{"id":139989,"project":{"id":215,"name":"Book Meeting Room...
You should be getting something like the following from the server:
handler({"issues":[{"id":139989,"project":{"id":215,"name":"Book Meeting Room"}}]});
I ran into a similar issue and found that passing the callback or jsonp parameter with a handler name solved it. More info here in the redmine documentation.
In jQuery this would be my request:
$.get('someurl', {'callback': 'foo'}, function (data, status, xhr) {
... request logic ...
}, 'jsonp'
);
...and this would be your request with the callback parameter:
$http({
method: 'JSONP',
url: url,
callback: 'something'
}).
success(function(data, status) {
console.log(data);
}).
error(function(data, status) {
console.log("Error "+status);
});
This may not help you with the authentication woes but will hopefully get you past this error.

Categories

Resources