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/
Related
I have seen many examples on how to get posts from wordpress websites, but I haven't seen any example on how to post a post into a wordpress website using WP REST Api, except this update title example that is incomplete:
$.ajax( {
url: wpApiSettings.root + 'wp/v2/posts/1',
method: 'POST',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
data:{
'title' : 'Hello Moon'
}
} ).done( function ( response ) {
console.log( response );
} );
Could someone please show me a full working example of how to post a post into a remote wordpress website, thank you.
$( document ).ready(function(){
//alert('a');
//Perform Ajax request.
$.ajax({
url: "http://apibind.com/wp/wp-json/wp/v2/posts",
type: "POST",
headers: { "Authorization": "Bearer ***************');},
data: {"title" : " tttt","content":"some test content" },
success: function (data) {
console.log(data);
},
error:function(data){
console.log(data);
}
});
});
I develop a mapping app, the front-end is created with Flask. When searching the external backend (create with the django framework) with ajax requests. I would like redirect the url after return from the ajax response (if success or not). But, I don't know the best way for this !
submitHandler: function () {
/********* GET USER TOKEN WITH AJAX REQUEST**********/
$.ajax({
method: 'POST',
url: "url for get token",
data: {
username: $('#email-log').val(),
password: $('#password-log').val()
},
success: function (response) {
if(response.d == true) {
localStorage["username"] = $('#username-log').val();
localStorage["user_token"] = response['token'];
window.location = "{{url_for('maps')}}";
}
},
});
},
Where do I do this redirection?
In ajax request, in the form action = "", using url_for() somewhere ?
I'm lost in all these methods
If you only want to redirect after Ajax success you can do this:
$.ajax({
// do what you want,
success: function(){
window.location.href = "/url/for/route/" //redirect url
// or
window.location.replace("url/for/route")
}
});
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
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.
What is the use of beforeSend Function in jQuery Ajax?
How to use the jQuery function?
I am Using jQuery 1.6.0, and Using Jersey API (Restful Web services) on Server Side.
$.ajax({
type: "GET",
url:ajax_url,
dataType: "json",
contentType: "application/json",
async: false,
beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', "Basic "+ btoa(username + ':' + password));
},
success:function(data){
alert(data.groups);
},
error:function(xhr,err){
console.log("readyState:"+xhr.readyState+"\nstatus: "+xhr.status)
alert(xhr.responseText)
alert("Service is not Available , Try it after Some time");
}
});
Java Code :
#GET
#Produces(MediaType.APPLICATION_JSON)
public String authCheck(){
return "({"groups": "success"})";
}
Whenever I am sending for authentication I am getting success response.
How to use beforeSend function and does we need to do any thing on server side?
Generally beforeSend() is used to do some stuff before actually send AJAX request like set Custom header and etc...
but your need is different that you want to authenticate user before your ajax call than
create one another function which give you User is authenticated or Not in Response
suppose this function is checkAuth();
so use
type: "GET",
url:ajax_url,
dataType: "json",
contentType: "application/json",
async: false,
beforeSend:checkAuth,
.
.
.