Got a 404 error on my JavaScript route - javascript

I'm using Play! v2 and I added a JavaScript method on my page that tries to retrieve data from the server. The client sends 2 information, a deal and a boolean (withDebug).
My routes file:
GET /:deal/tailLog controllers.MyController.tailLog(deal: String, withDebug: Boolean)
I also tried that, without success:
GET /:deal/tailLog?withDebug=:withDebug controllers.MyController.tailLog(deal: String, withDebug: Boolean)
MyController class contains the following methods:
public static Result tailLog(String deal, Boolean withDebug) {
...
}
public static Result javascriptRoutes() {
response().setContentType("text/javascript");
return ok(Routes.javascriptRouter("jsRoutes",
...,
controllers.routes.javascript.MyController.tailLog()
));
}
And finally, the JavaScript call is:
function tailLog() {
var withDebug = $("#logs-debug").is(':checked');
jsRoutes.controllers.MyController.tailLog('mydeal', withDebug).ajax({
...
});
}
When this method is called, my application is calling the URL http://localhost:9000/mydeal/tailLog?withDebug=false, which is the URL pattern I want, but fails with a 404 error message.
Note that before I added the withDebug parameter, everything was working fine.
What am I doing wrong?
Thanks.

In Play 2.0.4, you must use 0/1 to bind boolean parameters (and not false/true)
A little update in your javascript should fix this error:
var withDebug = $("#logs-debug").is(':checked') ? 1 : 0;

Related

Javascript Object returns empty when called

a piece of my code
const channels = fauna.paginate(q.Match(q.Index("channels"), "true")) // Query FaunaDB database for channel list => create constant called users containing results
const channelList = channels.each(function (page) {
// Logs the page's contents,
// for example: [ Ref(Collection("test"), "1234"), ... ]
console.log(page);
});
works fine and behaves how it supposed to. however, when I try to call "channelList" from elsewhere in my code it returns {}
The console.log in the first piece of code returns what it is supposed to as well so I dont think there is anything wrong with the first chunk of code.
Here is a piece of code where I attempt to call this object
let options = {
options: {
debug: config.twitchConfig.options.debug
},
connection: {
reconnect: config.twitchConfig.options.reconnect,
secure: config.twitchConfig.options.secure
},
identity: {
username: config.twitchConfig.connection.username,
password: config.twitchConfig.connection.password
},
channels: [JSON.stringify(channelList)] // Attempt to call here, Returns {} (Empty object)
};
Is there something I'm missing? is this even possible in the first place? if its not possible whats another method i can use to achieve the same result?
Edit: From what I can gather, channelList is based off of the page response, and it seems like the page response is private to that function and cannot be referenced outside of the function. what can I do to either make it referencable outside of the function or create a constant/variable that can be accessed outside of the function containing the same information
channelList is not a function, more like it is the result of a function that prints the channels.. so after printing what is assigned to it is the result..
Try assuming it as a function and then invoking it:
const channelList = ()=>{
channels.each(function (page) {
// Logs the page's contents,
// for example: [ Ref(Collection("test"), "1234"), ... ]
console.log(page);
});
};
channelList();
Assuming the code itself does what you want it to, this should do the job
After much trial and error my answer was way simpler than me or anyone else I have spoken to thought.
Here's a step by step guide on how to fix the problem yourself if you're experiencing the same issue as I was.
Use a variable instead of a constant
declare your variable before setting it by placing var channelList; somewhere above your code that sets it
set the previously declared variable with a simple channelList = page
console.log(channelList); just to make sure its all working
Finally, call the variable where you want it, and voila! you have your variable properly called and it doesn't return {} or Undefined or [Object Object]

Binding javascript code to android code with objects parameters

As the title suggests, I'm trying to bind javascript code to my android app so I can react in my app to an event/message that my website is sending.
After reading the official android documentation related to javascript binding I managed to easily implement it.. as long as it's a string.
What is working fine?
I implemented the following code in my app:
/** Instantiate the interface and set the context */
class ClientInterface(private val mContext: Context) {
/** Show a toast from the web page */
#JavascriptInterface
fun postMessage(message: String) {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show()
}
}
if the parameter of the 'postMessage' function is a String and I'm passing a String from my javascript as a parameter, everything is fine. it's passing the string.
my problem is that I am trying to get a JSONObject instead of a String, and it's not working.
I tried casting everything I thought might work.. JSONObject / JSONObject? / Any / Any? / Object / Object? and so on..
when I'm sending an object on my javascript, nothing seems to work. all I get in my app is a null response.
anyone ever tried something like that? what am I missing?
P.S - here's my javascript code for reference:
var objectMessage = {
type: "quote",
code: "My name is Inigo Montoya. You killed my father, prepare to die!"
}
window.CLIENT.postMessage(objectMessage);
You can't pass an object only primitive!
So you need to stringify your object.
var objectMessage = {
type: "quote",
code: "My name is Inigo Montoya. You killed my father, prepare to die!"
}
window.CLIENT.postMessage(JSON.stringify(objectMessage));

Java variable not updating inside Javascript's setinterval

Hi I am currently doing a web application where my Java code will constantly fetch data from a database and update its own static variable. I can confirm that the variable is being updated constantly when I output it in the console, however when I want to use this variable inside my Javascript function (which is inside my .jsp code), it always takes the initial value and never gets updated despite being inside a SetInterval function.
The following is the Javascript segment where I access the Java static variable.
function moveMarker(map,marker){
setInterval( function(){
document.write(<%=DbManager.latitude%>);
},5000);
}
The DbManager.latitude variable is always in it's default value despite being changed consistently when the web app is running. I look forward to any answers I can get to fix this issue, or perhaps alternatives to what I'm trying to achieve.
Better you can goahead with AJAX for this.
<script type="text/javascript">
$(document).ready(function() {
setInterval(ajaxCall, 5000); // 5 MS
});
function ajaxCall() {
$.ajax({
type: "POST",
url: "/getUpdate/" ,
success: function(result) {
document.write(result);
}
}
});
</script>
Call your servlet/controller from AJAX and get the updated value
#Controller
#RequestMapping(value = "clause")
public class ClauseController {
#RequestMapping(value="getUpdate")
#ResponseBody
public String selectClause(ModelMap model) {
DbManager dbManager = DAO.getDbManager;
return "dbManager.latitude";
}
}

Active X control custom events fail to fire in Javascript

I have written a custom active X control using the IDispatch interface that I would like to communicate with javascript. I have successfully gotten the javascript -> COM path working; I can call a javascript function on my active x object and receive a corresponding INVOKE call in my dll.
To receive events on the javascript side, I am following the advice in this article: http://jeffcode.blogspot.com/2008/02/how-to-create-activex-control-that.html
When I load my test page, I get a call to FindConnectionPoint followed by a call to Advise, as I would expect. When I call Invoke on the interface given by Advise, I get a success status message, but nothing happens on the js side!
This is the javascript code I am using to test event handling:
function FooActiveX::ReceiveMessage(msg)
{
alert(msg);
}
Interestingly, if I remove that, I don't get the calls to FindConnectionPoint or Advise anymore, so it's doing SOMETHING.
Any advice on how to debug this problem or things to try would be very helpful. Thank you!
My idl interface definition file looks like this:
[
uuid("1bf6bb1a-3232-11e4-a195-a6c5e4d22fb7"),
version(1.0),
]
library FooControlLib
{
interface IFooControl;
dispinterface DFooControlEvents;
importlib("stdole2.tlb");
[
uuid("1bf6bb1a-3232-11e4-a195-a6c5e4d22fb8"),
hidden
]
dispinterface DFooControlEvents
{
properties:
methods:
[id(DISPID_RECEIVEMESSAGE)] void ReceiveMessage( [in] BSTR msg );
}
[
odl,
dual,
uuid("1bf6bb1a-3232-11e4-a195-a6c5e4d22fb9"),
oleautomation
]
interface IFooControl : IDispatch
{
[id(DISPID_SENDMESSAGE)] HRESULT SendMessage( [in] BSTR msg);
}
[
uuid("1bf6bb1a-3232-11e4-a195-a6c5e4d22fc0")
]
coclass FooControl
{
[default] interface IFooControl;
[source, default] dispinterface DFooControlEvents;
}
}
EDIT: It seems that the problem is related to the parameter in the ReceiveMessage method. If I remove the "msg" parameter, the alert box will display properly.
Found my problem. The arguments to the method are passed to Invoke as an array of VARIANTARG structures. I had set the value, but not the vt member of that struct, which identifies the type of the parameter. I don't know why invoke returned an OK status.

CHtmlView: How to get data on click?

I have an MFC application that uses CHtmlView. It displays some text in html format from some temp html file. Is it possible to handle mouse click on a paragraph to send some data to the program? I understand that javascript can be used to handle click, but how to pass the data from javascript function to the application??
Thanks.
It is possible to cleanly call the containing application from within the Javascript of the HTML page. At the Javascript level the MSHTML interface that is doing the actual work of the CHtmlView provides an "external" object that acts as a way back to the calling application.
Suppose we want to add a method "someCall()" that can be called from Javascript, and that the method takes a string as an argument. In JavaScript we would call it with something like
external.someCall("An example string");
In the MFC application, we need to write a CCmdTarget derived object to act as the implementation of the "external" object as a dispatch-based COM object, something like:
class TestExternal : public CCmdTarget
{
public:
TestExternal()
{
EnableAutomation();
}
void SomeCall(LPCWSTR str)
{
// This is where we get called when the Javascript runs...
}
private:
DECLARE_DISPATCH_MAP()
};
BEGIN_DISPATCH_MAP(TestExternal,CCmdTarget)
DISP_FUNCTION(TestExternal,"someCall",SomeCall,VT_EMPTY,VTS_WBSTR)
END_DISPATCH_MAP()
To tie this implementation of "external" with the HTML view, in a class derived from CHtmlView you need to over-ride OnGetExternal() and to point it to an instance of TestExternal that lives at least as long as the CHtmlView:
class TestHtmlView : public CHtmlView
{
// Usual implementation stuff goes here...
public:
HRESULT OnGetExternal(LPDISPATCH *lppDispatch)
{
*lppDispatch = m_external.GetIDispatch(TRUE);
return S_OK;
}
private:
TestExternal m_external;
};
Note that I haven't actually tested this, but it seems about right from memory ...

Categories