Calling JS Function From C# Not Working - javascript

Afternoon,
I have been able to call JavaScript functions from my C# before and it worked fine. For some reason this time the function is not getting hit when i breakpoint the code.
So this is my C# method.
public void tester()
{
string returnResult = HttpContext.Current.Session["result"].ToString();
Page.ClientScript.RegisterStartupScript(this.GetType(), "alerify", "alerify('" + returnResult + "');", true);
//ScriptManager.RegisterStartupScript(this, this.GetType(), "alerify", "alerify(" + returnResult + ");", true);
}
You can see that i have tried different methods but the function is still not getting hit.
This is the JavaScript function i want to call.
function alerify(e) {
alert(e);
if (e == "InvalidDates") {
alertify.error("gfgsdggfsdgfsdfd");
}
}
I am thinking that i am missing somthing but i just don't know what.

As it's a string value it needs apostrophes (or quotation marks) to be a string literal in the JavaScript code:
... "alerify('" + returnResult + "');" ...

Related

Pass 2 arguments in Handler javascript function in codebehind

I am trying to pass 2 parameters in Handler call in codebehind but I can't get it to work. Here is my code:
X.Msg.Confirm("Confirm", "The field has " + dependency.Count() + " dependent fields. Are you sure you want to proceed? (The dependent fields will be deleted also)", new MessageBoxButtonsConfig
{
Yes = new MessageBoxButtonConfig
{
Handler = "App.direct.UC.DoYesDeleteDepField('" + fieldname + "," + dependency + "')", //ERROR LINE
Text = "Yes"
},
No = new MessageBoxButtonConfig
{
Handler = "",
Text = "No"
}
}).Show();
Error:
System.ArgumentException: DirectMethod: 'DoYesDeleteDepField', The parameter 'dep' is undefined at Ext.Net.DirectMethod.Invoke(Object target, HttpContext context, ParameterCollection args) at Ext.Net.DirectMethod.Invoke(Object target, ParameterCollection args) at Ext.Net.ResourceManager.RaisePostBackEvent(String eventArgument)
[DirectMethod]
public void DoYesDeleteDepField<T>(string fieldname, List<MyDependenciesClass> dep)
{....
You are passing:
App.direct.UC.DoYesDeleteDepField('fieldname,dependency')
the way you written it.
To pass this:
App.direct.UC.DoYesDeleteDepField('fieldname', 'dependency')
you should draw the line like this:
"App.direct.UC.DoYesDeleteDepField('" + fieldname + "', '" + dependency + "')"
For further information on this answer: the initial parameters were being read as a string, thus the javascript threw 'undefined'.
The solution works due to String Concatenation, which is a very important concept on OOP.
For more on string concatenation consult: How to Concatenate Multiple Strings (C# Programming Guide)

Android calling JavaScript functions in WebView with API <= 18

This question isn't new here, but still I cannot get it where I go wrong. I just want to evaluate some javascript.
My simple code looks like:
WebView wv = new WebView(context);
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
JavaBridgeToJS bridgeToJS = new JavaBridgeToJS();
wv.addJavascriptInterface(bridgeToJS, "javaCallback");
String src = "javascript: var result= 3 + 3; window.javaCallback.resultOfAdd(result)";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
Log.d(TAG, "Newer than JellyBean.");
ValueCallback<String> result = new ValueCallback<String>() {
#Override
public void onReceiveValue(String value) {
Log.d(TAG, "Got in onReceiveValue: " + value);
}
};
wv.evaluateJavascript(src, result);
}else {
Log.d(TAG, "Older than Kitkat");
wv.loadUrl(src);
}
and
public class JavaBridgeToJS {
#JavascriptInterface
public void resultOfAdd(String result) {
Log.d(TAG, "Here is: " + result);
}
}
It is working good for API 19+, even without if with checking of version (I mean that it works with loadUrl as well as with evaluateJavascript)
But when I try API 18, the callback in java is not called.
I have looked around (remove line break, or quotes missing (possibly infected, watched on phone), and so on...)
So far I have no luck. Any idea?
EDIT
Well, I have added code for handling errors inside of JS in case, that I have missed something.
wv.setWebChromeClient(new CustomWebChromeClient());
as
public class CustomWebChromeClient extends WebChromeClient {
private CustomWebChromeClient() {
super();
}
#Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
UILog.d(TAG, consoleMessage.message());
return super.onConsoleMessage(consoleMessage);
}
}
and I was suprised with:
E/dalvikvm: Could not find class 'android.webkit.PermissionRequest', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super
VFY: unable to resolve check-cast 1896 (Landroid/webkit/PermissionRequest;) in Lcom/example/WebViewDoll$CustomWebChromeClient;
E/dalvikvm: Could not find class 'android.webkit.WebChromeClient$FileChooserParams', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super
VFY: unable to resolve check-cast 1899 (Landroid/webkit/WebChromeClient$FileChooserParams;) in Lcom/example/WebViewDoll$CustomWebChromeClient;
E/dalvikvm: Could not find class 'android.webkit.PermissionRequest', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super
D/WebViewDoll: Uncaught TypeError: Cannot call method 'resultOfAdd' of undefined
E/Web Console: Uncaught TypeError: Cannot call method 'resultOfAdd' of undefined at null:1
I did not find an answer why this is not working, but with help of this using js in webview I was able to make a workaround ...
I have to inject my js into a html page. So my wrapper arround js looks like:
String src = "<html><head><script type=\"text/javascript\">" +
"function myFunction()" +
"{" +
" javaCallback.resultOfAdd(3+3);" +
"}" +
"</script></head><body onload=\"myFunction()\"/>" +
"</html>";
and instead of wv.loadUrl I have used:
wv.loadData(src, "text/html", "UTF-8");
This combination have same result as needed.

Passing json object from Android/iOS native to javascript

Is there any way to pass JSONObject from android to javascript. We are using WebView.evaluateJavascript mehtod and are able to send only String type object. In JS if we are checking the method paramter;s typeof(data) then it is displaying as string but in iOS it displays typeof(data) as OBJECT.
In both android and iOS we are passing String and NSString.
JS method is:
response: function(id, err, data) {
var dataObj;
if(typeof(data) == 'string' ){
dataObj = JSON.parse(data || '{}');
}
}
Android call:
String responseStr = "{\"ok\":\"ok\"}";
String nativeToWebMethod = "javascript:window.nativeService.response("1",'','"+responseStr+"')";
webView.evaluateJavascript(nativeToWebMethod, null);
Just send it as if you were loading a url:
private void loadJS(String jsonResponse){
mWebView.loadUrl("javascript:" + "(function(){" + "yourJsMethodName" + "(" +jsonResponse + ");" + "})()");
}
This will execute a JS Code that will call a function called yourJsMethodName and will pass the JSON as parameter.
Consider execute the last code in the Main Thread
I find out the issue, it was in the way I was sending data:
String nativeToWebMethod = "javascript:window.nativeService.response("1",'','"+responseStr+"')";
should be replaced with
String nativeToWebMethod = "javascript:window.nativeService.response("1",'',"+responseStr+")";
removed single quote around the responseStr.

Android WebChromeClient integration with javaScript gives Unexpected Illegal token

Home.java
public void updatePhoto(Bitmap bitmap) {
String bitmapStr = BitMapToString(bitmap);
Home.currentBmp = bitmap;
String parameter = "data:image/jpeg;base64," + bitmapStr;
webView1.loadUrl("javascript:var script = document.createElement('script');" +
"script.type='text/javascript';script.src='form.js';" +
"script.onload=" + "processImage('" + parameter + "');" +
//"script.onload=" + "processImage(':/;//');" +
"document.getElementsByTagName('head').item(0).appendChild(script);");
Log.v(parameter, "QWERTY");
Log.v("QWERTY", parameter);
}
form.js
function processImage(img) {
alert('process image end');
}
this function converts the Bitmap object into a base64 String, then executes an external javascript function (form.js -> processImage(var)). However it doesn't execute processImage and gives an Uncaught SyntaxError: Unexpected token ILLEGAL. However when i uncomment the
//"script.onload=" + "processImage(':/;//');" +
line and comment in
"script.onload=" + "processImage('" + parameter + "');" +
it seems that the function is working well. The possible value for parameter variable would be
data:image/jpeg;base64,iVBORw0KgoAAAANSHUEgAAA......
What I am asking is how am I gonna make the javaScript function work with the parameter? Thanks!

Jquery ajax functions stopped working

Ive been working on some jquery within a a page.
Now all of a sudden the post functions seem to have stopped working?
function deleteRow(OrderNo, LineNo) {
alert(OrderNo + ";" + LineNo);
$.ajax({
type: "POST",
url: "Ajax.aspx/DeleteRow",
data: '{' + 'OrderNo:"' + OrderNo + '",' + 'LineNo:"' + LineNo + '"' +
'}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//$("#item").val(msg);
var data = jQuery.parseJSON(msg);
if (!data.error) {
$('#' + LineNo).remove();
}
else {
alert("Error" + " " + data.error);
}
},
error: function (msg) {
alert('Failure: ' + msg);
}
});
}
This is a jquery function which gives an error back 'Failure [object Object]'
the function DeleteRow does exist in Ajax.aspx and does work. Cant understand why all of a sudden the post functions would stop working??
[WebMethod]
public static string DeleteRow(string OrderNo, string LineNo)
{
SqlConnection myConnection = new SqlConnection(connStr);
myConnection.Open();
//Check if param exisits
string SQLst = "Delete from Saved_Order_Import where [Order No] = '"+OrderNo+"' And [Line No] = '"+LineNo+"'";
try
{
SqlCommand myComman = new SqlCommand(SQLst, myConnection);
myComman.ExecuteNonQuery();
}
catch (Exception ex)
{
myConnection.Close();
return "{\"error\":\"Error Line Not Deleted" + ex.ToString() + "\"}";
}
myConnection.Close();
return "{\"Success\":\"Line Deleted\"}";
}
console log
abort: function ( statusText ) {
always: function () {
complete: function () {
done: function () {
error: function () {
fail: function () {
getAllResponseHeaders: function () {
getResponseHeader: function ( key ) {
isRejected: function () {
isResolved: function () {
overrideMimeType: function ( type ) {
pipe: function ( fnDone, fnFail ) {
promise: function ( obj ) {
readyState: 4
responseText:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"><title>
</title></head>
<body>
<form name="form1" method="post" action="Ajax.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZAZAz479BJ9BS5KpwM0PauBgztmI" />
</div>
<div>
</div>
</form>
</body>
</html>
"
setRequestHeader: function ( name, value ) {
status: 200
statusCode: function ( map ) {
statusText: "parsererror"
success: function () {
then: function ( doneCallbacks, failCallbacks ) {
__proto__: Object
You problem is on this line:
'{' + 'OrderNo:"' + OrderNo + '",' + 'LineNo:"' + LineNo + '"' +
'}',
It should be like this:
'{' + '"OrderNo":"' + OrderNo + '",' + '"LineNo":"' + LineNo + '"' +
'}',
Notice the missing opening " before OrderNo:" and before LineNo:". The fix will produce a valid JSON string:
'{"OrderNo": "OrderNo Value", "LineNo": "LineNo Value"}'
It's suprisingly uncommon the knowledge that those double quotes are required for valid JSON.
Based on the response you posted, the server output was a HTTP Status 200 with a HTML Form as the response. Was this the desired format of the response?
You're telling the AJAX function to parse the response as JSON but no JSON came back from the request. Look at your console log. The exception is a parser error.
There are lots of improvements that could be brought to your code. I will try to cover at least some of them that are bugging me when hovering over your code at first sight.
The first thing that worries me is that your page method returns a string, in which you are manually writing some JSON. That's something you should never do. You should never manually serialize/deserialize anything. In any language. Never. You can read the following article to understand why. Page methods can return strongly typed objects and the ASP.NET infrastructure will take care of properly serializing them into JSON so that you don't have to worry about it. So let's start by introducing a model that your page method could return:
public class Result
{
public bool Success { get; set; }
public string ErrorMessage { get; set; }
}
As you can see in this model we have a boolean variable indicating the success or failure of the page method and a string variable containing the error message in the event of failure.
The next thing, and probably, the worst with your code, is the SQL injection vulnerability present in your ADO.NET snippet. Let's fix that by introducing parametrized queries and returning the model we have just defined:
[WebMethod]
public static Result DeleteRow(string OrderNo, string LineNo)
{
try
{
using (var myConnection = new SqlConnection(connStr))
using (var myCommand = myConnection.CreateCommand())
{
myConnection.Open();
myCommand.CommandText = "DELETE FROM Saved_Order_Import WHERE [Order No] = #OrderNo AND [Line No] = #LineNo";
myCommand.Parameters.AddWithValue("#OrderNo", OrderNo);
myCommand.Parameters.AddWithValue("#LineNo", LineNo);
myCommand.ExecuteNonQuery();
}
}
catch (Exception ex)
{
return new Result
{
Success = false,
ErrorMessage = "Error Line Not Deleted" + ex.ToString()
};
}
return new Result
{
Success = true
};
}
The last step is to clean the client side code. Here you I would recommend you to use the JSON.stringify method to properly JSON serialize the javascript literal instead of using some string concatenations to manually build your JSON (read the article I have linked previously in my answer to understand why you should never manually serialize/deserialize anything => you should always use a proper qserializer for the given format).
$.ajax({
type: 'POST',
url: 'Ajax.aspx/DeleteRow',
data: JSON.stringify({ OrderNo: OrderNo, LineNo: LineNo }),
contentType: 'application/json; charset=utf-8',
success: function (msg) {
// Notice how we use msg.d here. The ASP.NET Page Methods
// infrastructure will JSON serialize the response using this property:
// {"d":{"Success":"true"}}
var data = msg.d;
if (data.Success) {
$('#' + LineNo).remove();
}
else {
alert('Error ' + data.ErrorMessage);
}
},
error: function (msg) {
alert('Failure: ' + msg);
}
});
Also make sure that you have enabled page methods in the script manager of your page:
<asp:ScriptManager ID="scm" runat="server" EnablePageMethods="true" />
Remark: the JSON.stringify method is natively built-in modern browsers but if you need to support legacy browsers you could include the json2.js script to your page.
PHP devs (like me) who deal with jQuery, Ajax and other frontend technologies: "Suddenly not working" could mean that you added some debugging, some "echos" (PHP wrong way of debugging) and you forgot to check the entire stack in order of removing it. That debugging code you let there will cost long painful days of reviews and tests. Have you ever considered that? Follow these steps:
0 - (Please: Developers start counting from zero) - Directly call backend using URL only, whenever possible, instead of using ajax. You can grab a testing tool like these ones QA guys use - and we should get used on them too - there are many. Grow up. Go find one. Talk to QA guys. Do something. Now! :-)! Look at what you received: is it a valid JSON? Is it a valid XML? Is it a valid JSON/XML plus something that should not be there? Is it a valid JSON/XML but not the one you expected to receive? Probably this step is enough.
1 - Get used to the following useful ajax snippet (http://craigsworks.com/projects/forums/showthread.php?tid=3829) :
$.ajax({
url: 'http://localhost/formengine/index.php?r=site/ajax',
success: function(response) {
console.log(response);
}
});
2 - Test any other jQuery behavior instead of the one you are building. This step is just to make you feel better and recover that everyday rationality that pays your salary. jQuery, Ajax, PHP, .net stack, Java stack: they are friendly, nice and do want to be working for you. Sometimes it's just matter of handling head stuff: either an offline CDN, a URL that is wrong, source location may be wrong, these ordinary, everyday stuff. Place the CDN URL to the navigation bar: you shall be able to read the entire lib in your browser screen. Click on this: https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
3 - In the deepest moment of your desperation, have you placed any alert somewhere where it's not legal? Do not say "no"! Go testing it: google chrome, F12, a developers perspective frame opens and at the top right you can see the smallest red flag ever - yes: the smallest. Hear the voices in your head: click on it. A meaningful message will appear. If it's the case, fix the issue.
4 - Drive all the necessary efforts towards having a well defined deploying process. Copying and pasting are not professional approaches - and, believe me, you are the most interested ones on doing things right. There are many "best practices" references regarding deploying, considering, of course, the technology stack you guys use: follow them.
Good Luck to you all! Hope it helps!

Categories

Resources