I am newbie to rails and currently trying to build a REST api on Rails.I am trying to connect to the rest web service from my android app.
This is the controller code that i am routing the request to.
class SessionsController < ApplicationController
def create
user = User.authenticate(params[:userid],
params[:password])
if user.nil?
message="Invalid Username/Password"
return message
else
sessionId=make_sessionId
return sessionId
end
end
def destroy
end
end
I am trying to hit the create action in the SessionController.The problem is that the response i get is the html of the view whereas what i am looking forward to is the 'message or the 'sessionId' from the controller.I deleted the view files after which i am getting the html with exceptions inside it.
Can someone let me know what i should here to get the response from the controller rather than returning the html inside the view at the client.?
If you have no render in your Controller, then rails will use the view related to the action name. Try "render :text => message" for example. I hope I understood your question correctly.
Related
I have an Odoo server that is a WebApp with a website functionality.
I need to implement an Android/iOS app that comunicate with this website functionality.
The website functionality is simple:
Take the intervention code.
check if the state of the intervention sheet is visible into website.
if yes, edit the intervention sheet, if no show error message.
So I want to take the intervention number from Android (for example) and send it by HTTP request and if I get a yes response continue the editing and other stuff in a Webview....if I get "error" show the error into Android Activity.
This is my controller on server that check Code:
#http.route(['/checkCodeAction'],
type='http',
auth="public",
methods=['POST', 'GET'],
csrf=True,
website=True)
def worksheet_code_details(self, **post):
worksheet = request.env['project.task.worksheet']\
.sudo()\
.search([('intervention_number',
'=',
post.get('intervention_number'))])
if worksheet and worksheet.state_id.is_visible_on_frontend:
return redirect(f'/worksheetReadValues/{worksheet.id}')
return request.render(
"website_project_task_worksheet.worksheet_code",
{'error_code': True}
)
The request.render load an xml template of Odoo.... I can intercept this call into a webview?
Or I need to implements another controller for Android that receive for example two response (error, url_with_worksheetid)... so if I get error I show a message, if a get an URL I call the webview with this URL.
I think it's better to return the error using an HTTP Error Status Code and you would be able to better catch that error status code in your Android or IOS controller.
In Odoo you could return a raw werkzeug response like this (the following example from an existing Android-IOS-Odoo integration done by me):
return werkzeug.wrappers.Response(body, status=403, headers=[
('Content-Type', 'application/json'), ('Content-Length', len(body))
])
or you could add an status argument to your already returned render call that it's already a lazy render response wrapper, like:
return request.render(
"website_project_task_worksheet.worksheet_code",
{'error_code': True}, status=403
)
My question looks stupid at first glance and all my searches pointed out window.location and other JS stuff or the externalWebPage plugin. That's not what I'm looking for.
From the JAVA code, when I catch one specific exception during execution of a custom plugin, I want to force the page to move to "logout.html". I don't want to execute callback.error() or to deal with the error inside code in my webpage in any ways. I only want my transaction to be cancel and a web resource to be loaded in the current web UI.
Is there any way to do that?
Thanks in advance.
The CordovaWebView offers a showWebPage function to load any url from the native code.
From the plugin you should be able to do
this.webView.showWebPage("logout.html", false, true, null);
Also offers loadUrl
this.webView.loadUrl("file:///android_asset/www/logout.html");
And you can also use loadUrl to execute javascript so you can run the window.location from there without a callback.
this.webView.loadUrl("javascript:window.location.href='logout.html'");
You will either need to add it to your main.js interface so that you can callup page changed () and handle it in your angular code.
This can be a simple onPageNeedsChanged Handler call where you retain the context on page change context and just call it whenever you need.
Or you can call the onError callback from the caller, if it is a consistent error callback context to move you there, but sounds like you don't want to do this route.
So the easiest answer then is to just launch your own Activity with a preloaded web url and a web view. You already have access to the activity, so just make your own native activity with a full web view in it, hard coded url and then launch your activity on error.
i.e. I do it for sending an email, but it could be your own activity
cordova.getActivity().startActivity(Intent.createChooser(emailIntent, "Send mail..."));
You may even be able to get a reference to the Cordova web view, but not positive on that, but I assume you could through the tree of objects.
Does that work for you needs?
If not can you elaborate on your hesitation to handle in the onerror callback. It's fairly straight forward. Maybe I can help you there as an alternative? Retaining the callingContext and just using callingContext.error(withkey or instructions or object) is not too bad.
A35ble.writeValueToPodCharacteristic(this.device.macAddress, true, this.bytesToSend,
function (response) {
console.log("Success: " + response);
callbackContext.device.notificationReceivedFromPod(callbackContext.device.arrayBufferToString(response));
},
function (response) {
console.log("ERROR: " + response);
alert("Error Sending NSM Message: " + response);
}
);
For example I made a cordova plugin called A35ble that manages my bluetooth stuff and in this response I just show alert.
My first question here, & a newbie to everything including coding.
I am using django-rest-framework to call a rest api from an android app I am building.
I am calling another view's method from the api_view method of django-rest-framework.
This works great with the GET method, but fails while using the POST method.
Below is the code:
View 1:
#api_view(['GET', 'POST'])
def get_dos(request):
return View2.get_data(request)
View 2:
def get_data(request):
#if I print request.body, it goes through without problems
#print request.body
#but commenting it fails when I read from request.POST.get below:
var1 = int(request.POST.get('test', 0))
#fails in the above statement.
I have tried commenting all middleware classes in the settings file, but no luck :(. I keep getting "You cannot access body after reading from request's data stream" error.
Am I doing something silly? Any assistance appreciated. Thanks!
I have seen background service in phonegap and integrated successfully but, I need to make an ajax call to get the data from server.This call need to be happen in background process without disturbing the UI. I am unable to do this using this plugin.
And my code look like,
$.ajax({
type: "POST",
url: "http://example.com/xxx",
dataType: "xml",
async:true,
success: function (data) {
var serializer = new XMLSerializer();
var xmlString = serializer.serializeToString(data);
var finalxml= xmlString.replace(/</g, '<').replace(/>/g, '>').replace('<br/>','\n');
window.localStorage.setItem("xml_Questions",finalxml);
}
});
This ajax call has to be done with async(background) call while user doing something..
But user is not able to do anything until get the response.So I have followed Background service plugin for phonegap, but I am not able to implement this method with that plugin.
Can we do this?
Can anybody help me?
Thank you in adv...
Your $.ajax code looks right to me, so it shouldn't block the app behavior.
I don't completely understand your question... user shoudn't be allowed to do anything until the localstorage.setitem is set? If this is right, you could use jquery to enable some kind of "NEXT" button after the setItem instruction, so the user won't be able to move on until the async call is done.
I am new to sencha touch and i want to consume soap web service in sencha touch.I have written code for this cause, but the problem is that I am getting just plain HTML content as response not the soap object. And I dont know how to call a specific method from web service to sencha touch.
Here's my code :-
Ext.Ajax.request({
method: 'get',
url: 'http://192.168.1.15:80/himanshu/helloworldwebservice.asmx',
success: function (response, request) {
alert('Working!')
alert(response.responseText)
console.log('Response:-'+response.responseText)
},
failure: function (response, request) {
alert('Not working!')
console.log('Response Status:- '+response.status)
}
});
EDIT:- Ok i got the idea to call a specific method from web service from here.Like i have HelloWorld() method which only returns a single string and my url is http://192.168.1.15:80/himanshu/helloworldwebservice.asmx.
I can call HelloWorld() method by setting my url like this :- http://192.168.1.15:80/himanshu/helloworldwebservice.asmx/HelloWorld
But its not working for me.Every time i run the program 'Not Working' alert generates and 500 is the response stats i gets.Please make me understand that how can i call methods from webservice.Thanx in advance.
You will not be able to consume your SOAP webservice in this way, since performing a GET request on the asmx url will just return you the HTML content for the page listing your webservice methods.
Consuming SOAP webservices relies on POST requests and need that you send a correct XML SOAP request. I may suggest you to use something like http://archive.plugins.jquery.com/project/jqSOAPClient to execute your SOAP calls and retrieve your data and then pass them back to your Ext code.
Hope this helps
Nacef
Your code is absolutely fine. I think you are sending HTML data from the server side. Do check the response in Chrome/Safari Developer Tools. Also, use console.log() function instead of alert() function for a better view.
Also, open this url: "http://192.168.1.15:80/himanshu/helloworldwebservice.asmx" in browser and "View source" of the page - you will see what exactly you are sending.
You can make use of : SOAP Data Proxy
http://www.sencha.com/blog/taking-a-look-at-the-new-sencha-soap-data-proxy