IPhone PHONEGAP- Https ajax for datatype - XML - iphone

I'm trying to get XML response from a secure https URL using Jquery AJAX call...
I'm getting the following error. Can anyone help me with the issue?
Unauthorized user
Here is my code
$.ajax({
type : "GET",
url : url + "?" + params,
error : function(jqxhr, status, error)
{
alert(status +"....."+error);
},
success : function(data, textStatus, jqxhr) {
var xmlDoc = jqxhr.responseText;
alert(xmlDoc);
}
});

Definitely need more details, but if you're sending a request to a secure url, you likely need to pass some kind of value to show you're logged in or some-such, like a cookie value. To pass a value, you may need to use the headers option:
$.ajax({
type : "GET",
url : url + "?" + params,
headers : {"COOKIE", "userID=h3j5o273h4"},
error : function(jqxhr, status, error)
{
alert(status +"....."+error);
},
success : function(data, textStatus, jqxhr) {
var xmlDoc = jqxhr.responseText;
alert(xmlDoc);
}
});

Related

Parse - How to insert data on parse server using REST Api using javascript?

I am integrating parse rest api to insert data on a table using jsp page. and using following code in javascript -
var headers = {
"X-Parse-Application-Id" : "myparseappid",
"X-Parse-REST-API-Key" : "myparserestapi"
};
// Variable to store data:
var userData = {
"name" : "anyname"
};
var data = JSON.stringify(userData);
// Send data:
$.ajax({
type : "POST",
url : "https://api.parse.com/1/classes/MyClassName",
data : userData,
contentType : "application/json",
headers : headers,
dataType:"json",
success : function(data, status, xhr) {
alert("data:" + JSON.stringify(data) + " status: "+ status);
if (status === "success") {
// Show success feedback:
alert("Data was saved successfully");
} else {
// Show error message:
alert("Your data didn't save. Please check that you are online. Status: "
+ status);
}
},
error : function(data, status, xhr) {
// Show error message:
alert("Your data didn't save. Please check that you are online. Status: "
+ status);
}
});
and for ajax i called following js -
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
Question - It is not saving the records whats wrong with above code. please suggest me?
Thanks in advance!

elgg api auth and user auth in header for REST?

hi I am new to elgg REST API.
I want to login and add post to wire for that I have method=wire.save_post
I learned from Google that api auth and user auth must be given in request header how?
I am doing a ajax for adding post to wire :
$("#post_text").submit(function() {
$.ajax({
type:"POST",
url:"http://elgg.amusedcloud.com/services/api/rest/json/?",
data:{ method:'wire.save_post', text : text_val, access : 'public', wireMethod : 'site', username : uname },
dataType:"json",
success: function(data) {
}
});
});
Normally an ajax call to the elgg webservice you are referring to should look something like this. Note that the api_key and auth_token are part of the request URL.
$("#post_text").submit(function() {
$.ajax({
type:"POST",
url:"http://elgg.amusedcloud.com/services/api/rest/json/?method=wire.save_post&api_key=1140321cb56c71710c38feefdf72bc462938f59f&auth_token=df123dfgg455666",
data:{
text : text_val,
access : 'public',
wireMethod : 'site',
username : uname
},
dataType:"json",
success: function(data) {
}
});
});
You didn't mention this but, when you say
... I learned from Google that api auth and user auth must be given in request header
is this in the context of using OAuth as an authentication mechanism? In which case, you will have to use the HTTP header Authorization to send the hash and signature. The above call would then be like this.
$("#post_text").submit(function() {
$.ajax({
type:"POST",
url:"http://elgg.amusedcloud.com/services/api/rest/json/?method=wire.save_post&auth_token=df123dfgg455666",
data:{
text : text_val,
access : 'public',
wireMethod : 'site',
username : uname
},
beforeSend: function(xhr){
xhr.setRequestHeader('X-Test-Header', 'test-value');
},
dataType:"json",
success: function(data) {
}
});
});
Note the changes in the url and addition of beforeSend property to the ajax object.
References:
http://docs.elgg.org/wiki/OAuth
http://api.jquery.com/jQuery.ajax/

Making POST Requests in Backbone.js

I have a RESTful server, which accepts url encoded parameters.
In my case, making a post request to https:// my server:8443/test/auth
Set the request header as
Content-Type : application/x-www-form-urlencoded
and passing the parameters
username=myusername
password=mypassword
works and gives the desired response.
I want to achieve the same using Backbonejs. So, I have
Person = Backbone.Model.extend({
initialize: function() {
this.on('all', function(e) { console.log(this.get('name') + " event " + e );
});
},
urlRoot: "https:// my server:8443/test/authauth",
url : function(){
var base = this.urlRoot;
return base + ""
}
});
var person = new Person({ "username" : "myusername" , "password":"mypassword" });
person.save(null, {
beforeSend: function(xhr) {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}}
);
But, it says that no username and password attributes are not present. Waht is the correct way to pass the paramteres in url encoded form in Backbone.js
Thanks

HTTPS request on iphone not sent to server

I am developing an application for iphone using phonegap.
When submitting ajax requests using HTTP everything works fine. But when i switch to HTTPS, the requests aren't sent to the server. The same code works for HTTP and HTTPS on android.
Below is the code i am using, thank you for your help in advance
var xmlRequest = $.ajax({
url : "https://test.com",
type : 'POST',
contentType : "text; charset=utf-8",
data : sr,
dataType : "text",
headers : { SOAPAction : '"'+soapaction+ '"' },
success : function( data, textStatus, jqXHR ) {
console.log("----- xmlRequest.responseText: " + xmlRequest.responseText);
SOAPClient._onSendSoapRequest(method, async, callback, sch, data);
},
error : function( jqXHR, textStatus, errorThrown ) {
console.log("----- xmlRequest.responseText: " + xmlRequest.responseText);
console.log("----- errorThrown = " + errorThrown + " textStatus = "+ textStatus );
ServiceErrorMessage(method, jqXHR.status, errorThrown, xmlRequest.responseText);
},
cache: false
});
Add this to the end of your AppDelegate.m file
#implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
#end

making jquery AJAX POST to resful API

I'm trying to convert a REST call using Cordova plugin to a JQuery AJAX POST. I don't have the JQuery code right, the call is getting a connection refused error (hitting localhost). I'm successfully making GET requests to my localhost, so there isn't a connectivity issue.
The REST API code:
#Path("/track")
public class TrackResource {
...
The method in TrackResource class i'm trying to hit :
#POST
#Path("{trackid}")
#Consumes("application/json")
#Produces("application/json")
public Response addToResource(#PathParam("trackid") String trackid, String bodyJson) {
The AJAX code:
var trackingJSON = JSON.stringify(tracking_data);
var urlAjax = "http://localhost:7001/ds/resources/track/" + trackid;
$.ajax({
type: "POST",
url: urlAjax,
data: trackingJSON,
beforeSend: function() { $.mobile.showPageLoadingMsg("b", "Loading...", true) },
complete: function() { $.mobile.hidePageLoadingMsg() },
success: function(data) { alert("ajax worked"); },
error: function(data) {alert("ajax error"); },
dataType: 'json'
});
I'm not sure if i'm using the data option in the ajax call correctly, but it's my understanding that is where you would put the data you want to pass server side.
I do have other GET calls to this same TrackResource class working, so i know the base part of the URL is correct. I know the trackid value is populated correctly as well.
If you're posting a JSON string make sure you also set contentType: "application/json".
var trackingJSON = JSON.stringify(tracking_data);
var urlAjax = "http://localhost:7001/ds/resources/track/" + trackid;
$.ajax({
type: "POST",
url: urlAjax,
contentType: "application/json",
data: trackingJSON,
beforeSend: function() { $.mobile.showPageLoadingMsg("b", "Loading...", true) },
complete: function() { $.mobile.hidePageLoadingMsg() },
success: function(data) { alert("ajax worked"); },
error: function(data) {alert("ajax error"); },
dataType: 'json'
});
I needed to use the router address of my computer, 192...., in order to hit my localhost... I was running the application on an actual Android device, however, I guess trying to use localhost or 127.0.0.1 in the AJAX call must have been causing issues.