I am making a webpage which on load makes 2 queries. However, results of both queries are coming at same time (the longer time) because one is blocking the other. Is there a way i can give a time delay so the other query is made only after the data from first query is loaded ?
Although the information you are providing are not sufficient, I am assuming you are making Ajax requests using Javascript. If that is the case you can use jQuery in order to have some code that will run only after the request answer is received (it might have an error)
$.ajax({
url: "URL for first request here",
method: 'Your request method (POST, GET etc)',
data: 'Here assign any data you need to send'
})
.done(function( data ) {
// This code will be called only if your request is successful
// data is the response from the server
// Perform 2nd Request
$.ajax({
url: "URL for second request here",
method: 'Your request method (POST, GET etc)',
data: 'Here assign any data you need to send'
})
.done(function( data ) {
})
.fail(function(){
});
})
.fail(function(){
// This code will be called only if your request fails
});
Related
on the node server i am sending data to angular as
res.json(data);
at angular i'm making http request to get the data
$http({
method: 'GET',
url: '/doctor/getDocData'
}).then(function(response) {
$scope.docData=JSON.stringify(response);
alert($scope.docData) //alert data
alert($scope.docData.name); //undefined
}, function(response) {
alert("error in loading data")
})
i want to sore the received data and individually print when ever i want. what am i doing wrong.Some one please explain. Thank You.
Don't JSON.stringify() the response as that is turning it back into a string, rather than parsed, structured JSON. Just target the data directly. If you are using $http you may need to get at data of the response object:
$scope.docDate = response.data;
Then you can target the properties:
alert($scope.docData.name);
If for some reason your JSON is coming in as a string, instead of JSON, you can use JSON.parse(response.data) to parse the string to JSON. This assumes it's formatted correctly.
Hopefully that helps!
I have Laravel back-end API which can receive info from Ionic app. The problem I'm facing is sending an array of data through POST request...
This is my method:
Injection.ResourceFactory.postResource($http, SERVER, 'order/addToBasket', {
token: localStorage.getItem('token'),
quantity: $scope.quantity.value,
product_id: $scope.productId,
condiments: [$scope.condiments]
...
and postResource method is:
postResource: function ($http, SERVER, route, parameters, data) {
return $http({
method: 'POST',
url: SERVER + route,
data: data,
params: parameters
})
},
So everything is pretty straightforward, and works locally perfect. When I transfer everything online, suddenly the condiments aren't being sent. I have tried console logging it before and after the call, and as far as Ionic is concerned, an array is being sent...but for some reason not received?
When I make Input::all() call in Laravel i get only token, quantity and product_id. If I remove the brackets from condiments: [$scope.condiments] so that I send condiments: $scope.condiments I get only last item from the JSON being sent?
I can't seem to figure this one out...ideas?
I'm using
window.location.href = "some ajax call";
for exporting, but when there is large number of data (for eg.5000),I'm getting the result as "Request-URI Too Large "pathname"
does not allow request data with GET requests, or the amount of data provided in the request exceeds the capacity limit."
Can someone please provide me the solution for this problem?
Because url has its limitation, url length can not be exceeds 2,000 characters.
You should use POST request instead of GET and send data inside body.
CODE
$http({
type: 'POST',
url: exportUrl, //this should not contain data
data: "data=" + escape(JSON.stringify(exprotData)),
success: function (data) {
//success code
},
error: function (error) {
//error code
}
});
Hope this could help you. Thanks.
I have the following scenario, a page that will show different widgets with different data, the back-end is ASp.NET Web API 2 with SQL Server and EF + Repository Pattern + Unit Of Work.
If I have to show quite some data, including user profile and other information on top of the widgets information, what will you recommend:
make one big $http.get request that will return a big json and bind that one to the UI
or
each controller (service) when it loads will make it's unique call to back-end and get's the data it needs to display, that means each widget will make a call to back-end and retrieve it's values.
I just want to know what do you recommend as a best practice.
IMHO the best way is to separate every request into single service methods that way you can reuse just a part of it and not make server calls to load to whole data, check the angular-resource $resource to have a clean reusable service of server calls and not a bunch of $https arround your code:
example:
A service that points some url of your backend server
.factory('ClientService', ['$resource', function($resource){
return $resource('http://some_url/:controller/:method', null, {
"agents": { method: 'GET', params: { controller: 'agent', method: 'search' }, cache: false },
"query": { method: 'GET', params: { controller: 'client', method: 'search' }, cache: false },
"save": { method: 'POST', params: { controller: 'client', method: 'save' } },
"delete": { method: 'POST', params: { controller: 'client', method: 'delete' } }
})
}])
The use in the controller (Injecting ClientService as dependency)
// If i want to query the agents into a scope element
// that will call the url = http://some_url/agent/search
$scope.agents = ClientService.agents();
// If i want to query a single client i cant send adtional params
// as is a get request it will call http://some_url/client/search?id=5
$scope.client = ClientService.query({id:5});
// and you can event manage callbacks if you want to
// This will send the client object to the url = http://some_url/client/save
ClientService.save($scope.client).$promise.then(function(response){ alert(response) })
As you can see this way you can access just the things you need from the backend server not having to do all the callback response if you dont need to and in a reusable cleaner way
Info Angular Resource Docs
I think it depends...
If performance might be a problem you should think about what is best for your User... Will the overhead of making 4 HTTP requests affect the user experience in anyway? Also, would a one big request take too much time to retrieve info from the database?
However if you want just to use a developer perspective of the problem, I'd prefer doing 1 generic API call then calling it 4 times in Angular with different parameters for each Widget.
It is likely that making 4 requests will actually be faster. Not to mention the data can start being populated on the screen as it comes back, instead of needing to wait for the slowest service.
For the max number of concurrent AJAX requehttp://www.coderanch.com/t/631345/blogs/Maximum-concurrent-connection-domain-browsers
I would like to connect my Chrome Extension to my server, and send data back and forth. In particular, when the user clicks a button on the extension when he's navigating a certain URL, the server checks its database to see how many times that URL has been clicked, increment the count, and send the new count back to the user.
I know that sending the data to the server is possible with an AJAX request, but what about getting the data back from the server?
I think, you may use AJAX for getting updated count in a straightforward manner. For example (with jQuery):
$.ajax({
url: 'ajax/count.php?url=' + encodeURIComponent(newURL),
// dataType: 'json',
success: function(data) {
// parse you data received from server here
// data.count
}
});
So you can "send" new info as parameters of GET request, and get required information from server as http-response. The type of the data used to transfer the count is up to you. For example, this can be json (jQuery provides a shorthand method getJSON, which does the same customized ajax call).
If you don't want GET, you may use POST and specify data as follows:
$.ajax({
type: "POST",
url: "ajax/count.php",
data: { url: newURL },
success: function(data){
// ...
}
});
You will obviously need to use xhr / ajax
But with the present chrome-extension API you might get an error like this
No 'Access-Control-Allow-Origin' header is present on the requested resource.
In order to get around this problem one can simply put the link to the server on the permission array in manifest.json
"permissions": [
"tabs",
"http://www.myserver.dom"
],
For more detailed description see this documentation by Google