Cordova/Phonegap + Restful Api service + POST method - javascript

Can someone please put some light on how to connect your Cordova App to WebServices hosted on a server.
Method: POST method
URL: http://192.168.1.20:3030/Service1.svc
Service Method: Login
Here's what I have been trying(Username-Password verification):
function GetLogin() {
var UName = "12345678";
var UPassword = "12345678";
var jData = { UserName: UName, Password: UPassword };
alert('1');
$.ajax({
type: "POST",
url: "http://192.168.1.20:3030/Service1.svc/AmalatLogin",
data: JSON.stringify(jData),
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processdata: true,
success: function (msg) {
alert("Start");
document.getElementById("demo").innerHTML=msg.ResponseMessage.toString();
alert("End");
}
});
}
The above code is the javascript part. I have simply called the onClick="GetLogin()" on the submit button.
The problem is the code runs when the html file is being open using localhost( i.e. "http : //(localhost)/ aaa.html" (without spaces) or even by using Google Postman), but fails to do so when I run it from the directory(as in C:\www\aaa.html).
I have been searching for the solution everywhere. Please put some light on whether this is the limitation of technology or do I need to add some plugins.
Basically alert('1') is showing, other alerts are not.
They are all in same network

Related

JQuery AJAX POST produce null params in HttpServletRequest on Liberty profile

I have an older JAX-RS application that I'm running on IBM Liberty profile 16.0.0.4 with the jaxrs-2.0 feature. I'm getting some weird behavior that I can't explain, and need some help.
JAX-RS service code:
#Path("/loadData")
#POST
#Produces("text/plain")
public String loadData(#Context HttpServletRequest request) {
String id = request.getParameter("id");
String email = request.getParameter("email");
// add'l request processing code
}
JQuery code:
$(document).ready(function() {
var reqData = {id:"123456", email:"user#email.com"};
$.ajax({
type: "POST",
cache: false,
url: "http://localhost:9080/jaxrs/loadData",
data: reqData,
contentType: "application/json",
dataType: "json",
timeout: "30",
beforeSend: function() { ... },
success: function(data) { .... },
complete: function(data) { .... }
});
});
JQuery (in IE) Results:
request.getParameter("id") or ("email") = null;
request.toString() = org.apache.cxf.jaxrs.impl.HttpServletRequestFilter#f272c8dc
IOUtils.toString(request.getInputStream) = "id=123456&email=user1%40email.com"
SoapUI Results:
request.getParameter("id") = 123456 and ("email") = user#email.com;
request.toString() = org.apache.cxf.jaxrs.impl.HttpServletRequestFilter#de62638a
IOUtils.toString(request.getInputStream) = ""
I compared the RAW HTTP request in SoapUI and in IE's console, they both appears to be the same, as far as I can tell. So that's really got me confused, both JQuery and SoapUI are performing POST, but seems in IE's case the query stream is not being parsed into parameters, rather just maintained as a string. I've tried to mess around with the contentType and request declaration with no effects. I was originally using JQuery 1.7.1, but I tried on 3.1.1 with no effects. Has anyone seen this before. Any help or insights would be really great, thanks!
Try using "JSON.strigify" for params
$(document).ready(function() {
var reqData = {id:"123456", email:"user#email.com"};
$.ajax({
type: "POST",
cache: false,
url: "http://localhost:9080/jaxrs/loadData",
data: JSON.stringify(reqData),
contentType: "application/json",
dataType: "json",
timeout: "30",
beforeSend: function() { ... },
success: function(data) { .... },
complete: function(data) { .... }
});
});

JSON is not loading from server

I have a JSON file which i want to load end it is working fine when I am trying to load that file locally from my hard drive.
function save(){
$.ajax({
type: "GET",
url: 'cameras.json',
dataType: "JSON",
async : false,
success: function(data) {
var output = data.result["0"].Address;
alert(output);
},
error: function(data) {
alert("Error")
}
});
}
When i want to get access to this JSON file from my server:
function save(){
$.ajax({
type: "GET",
url: 'http://192.168.0.21:8080/json.html?type=cameras',
dataType: "JSON",
async : false,
success: function(data) {
var output = data.result["0"].Address;
alert(output);
},
error: function(data) {
alert("Error")
}
});
}
It's not working and I am getting error script.
What can be issue with that ?
I had the same problem.
Unfortunately, I think that remote (i.e. away from localhost) ajax calls are forbidden - or something similar - for security reasons.
Depending on your purposes, you can resolve the problem with a 2-steps action:
You can call - via ajax - a local server class which loads the remote file via REST web service.I solved this way...

Ajax not calling WCF service

I have hosted a service sucessfully on IIS.
Made sure that application is running.
I have given call to this service through ajax as:
var parameters = {
'EmailID':EmailID,
'Password':Password
};
$.ajax({
url: "http://localhost:85/MobileECommerceTesting/Service1.svc/validateLogin",
data: JSON.stringify(parameters),
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
cache: false,
success: function (Data) {
alert("asdsad");
},
error: function () {
alert("Error in Saving.Please try later.");
}
});
But its not giving call to the service.
What can be the problem?
EDIT:
EDIT2 :
Network Tab:
Take a look at this question:
the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
It might be related to your binding, which does not expect the content type application/json. You must use webHttpBinding to create "REST-like" services with WCF.

How to use a javascript ajax api in SAHI

We are using SAHI to do some benchmark tests. Currently they are collecting some times while exercising the application. These times are stored in a text file. We now have an app to collect and analyze this data. We have a restful API in .Net MVC 4. We created a javascript API thinking SAHI should be able to call the javascript methods and go on its merry way. We are having problems figuring out how to get SAHI to use JQuery properly and reference the API.
Javascript API Object:
;
var BenchmarkService = (function (options) {
var settings = $.extend({
serverName: "http://localhost"
}, options || {});
// public api
this.Benchmark = function (benchmarkName) {
var benchmark;
$.ajax({
type: "GET",
url: settings.serverName + '/BenchmarkServices/Services/Benchmark/' + benchmarkName,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (data) {
benchmark = data;
}
}
);
return benchmark;
};
this.SaveBenchmarkResult = function (benchmarkResultObject) {
var id;
var url = settings.serverName + '/BenchmarkServices/Services/SaveBenchmarkResult';
$.ajax({
type: "POST",
url: url,
data: JSON.stringify(benchmarkResultObject),
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) { id = data; }
});
return id;};
}
);
We would like be be able to call these methods within SAHI simply by referencing the .js file.
Is this possible?
Can you mention the Sahi version, your java version and os you are using. If you are a pro customer you can directly email to Sahi support team(after all you paid for the support)!
If you are using Sahi Os, let me know the version and I can look if it is possible or not.

PhoneGap AJAX Login

I am trying to build a log in application in PhoneGap. Ive been looking around a lot for similar things and I found them plenty... but I do not understand why this pieces of code does not work. When i press the logIn_btn it shows that It cannot reach the "success" piece of code. Thank you for your help!
$('#logIn_btn').click(function(){
user = $('#username').val();
pass = $('#password').val();
var ev = {'papa':true};
if(user!='' && pass!=''){
alert('trying login');
$.ajax({
url: 'https://localhost/server/IF/mobileApp/login.php',
type: 'post',
datatype: 'json',
data: ev,
success: function(data){
alert('asd');
}
});
console.line('done.')
} else {
alert('Please insert your username and password.');
}
});
Your server should not be https://localhost, it should be the name/ip of the server that you'll be logging into.
For additional information, look at your network tab with Chrome, or install a fail handler
$.ajax({
url: 'https://localhost/server/IF/mobileApp/login.php',
type: 'post',
datatype: 'json',
data: ev,
success: function(data){
alert('asd');
}
}).fail(function(jqXHR, textStatus){
});
console.line?I think it would be console.log. Rather try alerting the message. Also make sure the javascript executes after deviceready. Also localhost in android is a very different thing. Try using the ipadress or domain name to access the server.

Categories

Resources