Im trying to post a api using for my registration page and i have used $HTTP request to post data
it('login page to patient dashboard with existing email and password', function() {
console.log('entering login page')
browser.get('http://localhost:9000/login.html');
browser.executeScript(function(callback) {
var $http;
$http = angular.injector(["ng"]).get("$http");
console.log('http request')
return $http({
url: "http://int.eclinic247.com/reg/create-patient",
method: "post",
data: {"firstName":"jya","lastName":"raq"},
dataType: "json"
}).success(function() {
return callback([true]);
console.log('done')
}).error(function(data, status) {
return callback([false, data, status]);
console.log('oops not done!!!!')
});
})
element(by.model(Objects.locators.passwordBox)).sendKeys(Objects.login.password);
I see that the executeScript block doesnot run and there are no errors too...and the test case passes
Is this the way to post a http request using protractor...Please suggest me the proper to post a data to back-end directly
Any help is much appreciated...Thanks in advance
Related
I am developing a simple REST service in flask.
I have been trying to implement basic authorization.
Whilst, I can pass the username and password from the webpage using manual entry, I can't seem to read them from the header.
Here is my code:
On the server
def test():
if request.authorization and request.authorization.username == '***' and request.authorization.password == '***':
return "Authorized"
else:
return make_response('Authorization failed', 401, {'WWW-Authenticate': 'Basic realm ="Login Required"'})
On the client - using JavaScript
<script>
$(document).ready(function(){
$("#authButton").click(function(){
$.ajax({
xhrFields: {
withCredentials: true
},
headers: {
'Authorization': "Basic " + btoa("***:***")
},
url: "********:5001/",
type: 'GET',
success: function(){
console.log("success");
},
error: function (){
console.log("error");
},
});
});
});
</script
>
I have also tried the Javascript code without the xhr fields section, but for neither do I get anything returned at all.
If I don't send the headers from the client it works and simply asks for manual input of the username and password.
All I'm trying to do is authenticate from the header.
Any pointers would be very gratefully received.
The following javascript code works with the facebook login window appearing and allowing a user to login. The response values are captured and I know it works as alerts appear where setup but I cannot pass the value back to a controller method.
#RequestMapping(value ="/getAccessToken" , method = RequestMethod.POST)
public #ResponseBody String getAccessToken(#RequestBody String token){
System.out.println(token);
return token;
}
Javascript method called:
function doLogin() {
FB.login(function(response) {
alert(response);
console.log(response);
if (response.authResponse) {
alert(response.authResponse.userID);
alert(response.authResponse.accessToken);
var Token = response.authResponse.accessToken;
alert(Token);
$.ajax({
type: "POST",
url: "/HelloController/getAccessToken",
data: Token,
success: function (result) {
alert("Token");
},
error: function (result) {
alert("oops");
}
});
document.getElementById('loginBtn').style.
display = 'none';
getUserData();
}}, {perms:'manage_pages',
scope: 'email,public_profile', return_scopes: true});
};
The error I get is the following:
WARN 25660 --- [nio-8080-exec-9]
o.s.web.servlet.PageNotFound :
Request method 'POST' not supported
Appreciate responses.
The problem could be that you are using a new version of JQuery that sends request data as post form data instead of JSON as default. Try changing your ajax call to the following. The form data would not be recognized by your controller so if this is the case you should see a 404.
$.ajax({
type: "POST",
traditional: true,
url: "/HelloController/getAccessToken",
data: JSON.stringify(Token),
success: function (result) {
alert("Token");
},
error: function (result) {
alert("oops");
}
});
For reference see this post: Send JSON data via POST (ajax) and receive json response from Controller (MVC)
I'm getting error 404 while making ajax calls to my MVC controller
Here is the scenario:
I have 2 controllers:(PartnerControler,GettingSartedController)
While on the getting started page:http://mysite/GettingStarted/ I make the following ajax call:
$scope.SubmitRegistration = function() {
return $http({
url: '/partner/register',
method: 'POST',
data: $scope.UserAccount,
headers: {
'Content-Type': 'application/json'
I get a 404 on the call to the controller.
when I add the exact register method to the gettingstarted controller and change the ajax URL to the following it works:
url: '/gettingstarted/register',
When on web a page that uses a specific controller can you make calls to another controller?
How can this be achieved modifying the above script?
If you have separate js files, you can use workaround like below
<input type="hidden" id="registerurl" data-register-url="#Url.Action("register", "partner")"/>
and use this element's attribute as url to be used in your http call
var urlRegisterPartner = $('#registerurl').data('register-url');
I found another very good answer here in
https://stackoverflow.com/a/57274117/14181068 for
Ajax call to a different controller
This is the summary i got from the answer.
url on ajax
result
your/path
http://example.com/Home/your/path
~/your/path
http://example.com/Home/~/your/path or http://example.com/your/path
.../your/path
http://example.com/Home/.../your/path
../your/path
go up one directory level, resulting in http://example.com/your/path.
/your/path
http://example.com/your/path
sample
$.ajax({
type: 'Get',
url: "YourUrlHere",
data: { id: $('#inputId').val() },
})
.done(function (response) {
if (response.length > 0) {
}
})
.fail(function (error) {
alert(error.StatusText);
});
I have the services and within particular time duration if response is come than ok, other wise show error in popup.
Here is my service code:
angular.module('server', [])
.factory('api', function($http) {
var server = "http://myapi-nethealth.azurewebsites.net";
return {
//Login
login : function(formdata) {
return $http({
method: 'POST',
url: server + '/Users/Login',
data: $.param(formdata),
headers: { 'Content-Type' : 'application/x-www-form-urlencoded'},
})
},
};
});
Please tell me how can I use timeout property in services.
Read this post - How to set a global http timeout in AngularJs
Where you can set a timeout for your http calls.
You can inject the above factory in your controller and then make a call with success and error callbacks like below
api.login(formdata)
.success(function(){ alert("success"); })
.error(function(){ alert("error"); });
I'm not sure what is wrong with this request, but here is what is happening..
It appears the browser thinks I am making a cross-domain request, however I am making a request to the same page that the request comes from.
I call a function called search, and I am returned this in my google chrome dev console
EDIT:
The same thing happens when I load jquery-1.0.2.min.js
The server returns this: {"readyState":4,"status":404,"statusText":"error"}
Bad request search.js?1383509337:48
POST http://example.com/transaction/search jquery-2.0.3.js:7845
x.support.cors.e.crossDomain.send jquery-2.0.3.js:7845
x.extend.ajax jquery-2.0.3.js:7301
search search.js?1383509337:22
(anonymous function) search.js?1383422028:17
x.event.dispatch jquery-2.0.3.js:4676
y.handle jquery-2.0.3.js:4360
Function search()
function search(user, transactionDate, vendorField, amountField, accountField, departmentField, subDepartmentField, statusField, projectidField){
var url = "/transaction/search";
var ajax = jQuery.ajax({
type: 'POST',
dataType: 'json',
url: url,
data: {
user: user,
transactionDate: transactionDate,
vendorField: vendorField,
amountField: amountField,
accountField: accountField,
departmentField: departmentField,
subDepartmentField: subDepartmentField,
statusField: statusField,
projectidField: projectidField
},
beforeSend: function(data){
$('#searchSpinner').show();
console.log("Sending request");
},
success: function(data){
console.log("Successful request");
data = JSON.parse(data, true);
loadTransactions(data['response']['transactions']);
$('#searchSpinner').hide();
},
error: function(data){
console.log("Bad request");
$('body').html(JSON.stringify(data));
$('#searchSpinner').hide();
}
}).done(function(data){
console.log("finished");
$('#searchSpinner').hide();
});
}
Turns out the problem wasn't with the AJAX request, but a bad formed MySQL query was taking up too much resource time and the page time'd out.