call C# DLL in JavaScript - c#

I need to call C# DLL from JavaScript.This JavaScript page is within the Firebreath framewok.
The Flow should be
C#-->Wrapper-->Firebreath(C++)-->JavaScript.
then How to call this C# DLL or Class from JavaScript?
Please provide the solution for the same and let me know.

If DLL is a service. You only call it through JQuery ajax. Example:
$.ajax({
type:"POST" // Or GET
data:"id=12&name=abc",
dataType:"xml", // Default type - text
url:"/search/searchRecords", // URL of service
success: function (data){
// Do something
}
});
See: http://api.jquery.com/jQuery.ajax/

Related

Can i link c# and html normally

Hello every1 I am both new to this site and html so i wanna ask Can i do something like that
<link href="index.cs">
then use functions If possible how ??
I want to use functions that i declare in my c sharp file fro a button
like i dont know
<button id="button-test" onclick="helloworld()">click</button>
and i ll have a function in c sharp named helloworld like
void helloworld()
{
return;
}
Thnx for ur answers
You can use javascript functions or use asp.net.
You can call the function in controller class using ajax like this
$("#buttonId").click(function (e) {
e.preventDefault();
$.ajax({
url: "/Controller/MethodName/{parameter}"
});
});
you should use ajax and connect to c# controller via ajax
Visit this link:
AJAX Introduction!
Take a look at using Blazor or Blazor WASM:
https://dotnet.microsoft.com/en-us/apps/aspnet/web-apps/blazor
You can use C# client side.

How to make javascript interact with .aspx page?

I wish to run some asp.net code on a page generated by Wordpress. Is there a way using XMLHttpRequest() to make JavaScript interact with .aspx page just like a Servlet?
For example, I hope that I can do this in JavaScript:
xmlHttpRequest.open("POST", "http ://some.aspx", true);
send_request("request=add,1,2");
function getReadyStateHandler(xmlHttpRequest) {
//handle_response
}
How to write and configure such an asp page? Does someone know a good tutorial?
Thanks a lot!
You are better of using JQuery for this task.
It is Javascipt library that has exactly functionality that you re looking for
You can call your backend methods to invoke serverside action that would return JSON object. and then in done function process the result.
$.ajax({
url: "test.html",
context: document.body
}).done(function(result) {
var dataFromTheSreverside = result;
$( this ).addClass( "done" );
});
Here you can find a lot of examples

Call c# function of global class from javascript

I have a global function that returns some string.
I need to access to that function from JavaScript in one of the pages and set returned value to JavaScript variable.
Example:-
var jsvariable = <%GlobalClass.MethodReturningString();%>;
How to do that?
You cannot call C# function like this. you need to create a web service or webmethod
in order to call this function.
Please see this link it will help you.
http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx
Perform the following steps :
Right click on your website or web application and add a WebHandler. The extension for the webhandler is '.ashx'. Let's say you name your handler as 'Handler.ashx'
Open the Handler.ashx file and within the ProcessRequest method write the following :
context.Response.Headers.Clear();
context.Response.Write(GlobalClass.MethodReturningString(););
context.Response.Flush();
In your javascript, perform an ajax call using jQuery :
$.ajax({
url:'someUrl/Handler.ashx',
type:'GET',
success : function(data){
var someJavascriptVariable = data;
}
});
NOTE: This is a quick and dirty write so I'm not guaranteeing that the code will surely work, but it's a point for you to start with.
May i thick You cannot do this but u can access ur c# function using ajax
For Example
$.post('url', {parameter : parameter }, function (result) {
// here the result is your function return value or output
},"json");
URL FORMAT : '/pageurl/methodname'
First of all, as a clarification, it seems that you're not trying to actually call a C# method from Javascript, but rather render the return from a C# method inside the page so that it can be used as a Javascript variable value on the client side.
In order to do that you need to update you syntax like:
var jsvariable = '<%= GlobalClass.MethodReturningString() %>';
Please note that if your class is not in the same namespace as the inherited page from the code behind file, then you need to import its namespace like below:
<%# Import Namespace="GlobalClassNamaspace" %>
The namespace import can also be done globally (and it will be automatically available in all of the site's pages), using the web.config file as described here.
If you were to actually need to call a C# method from Javascript, which would have been needed if you wanted to dynamically use its results from client side code, then that could be accomplished through Ajax.

read Json data from a webservice using jQuery or C#

I have the following API which returns json data as follows:
API : http://data.mtgox.com/api/1/BTCUSD/ticker
JSON : {"result":"success","return":{"high":.......
using jquery i tried the following but it is not giving me the data.
$.getJSON('http://data.mtgox.com/api/1/BTCUSD/ticker', function (data) {
alert(data);
});
and
$.ajax({
type: 'GET',
url: 'http://data.mtgox.com/api/1/BTCUSD/ticker',
dataType: 'json',
success: function (data) {
alert(data);
},
error: function (error) {
alert(error + "error");
}
});
but in first i get no alert
and in second i get error alert.
How can I read this data using jQUERY or C#?
THanks
As Archer mentioned this won't work if you're not on the same domain. There is one way around this using CORS (http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) but you'd need to have control over the domain to set the required header or at least get the person in charge to do so.
The other option is to use JSONP which basically wraps the result in a function call that runs immediately when it returns by injecting a script tag. Problem is you lose nice things like error handling and you can't cancel the request.
I tried your problem. The error showed in Google Chrome Javascript Console was Origin http://localhost:1564 is not allowed by Access-Control-Allow-Origin
Check the answer to this Question and find your way out.
Is live data required ? Like "I must see the data as current as from this exact second" ?
If not (probably that is the case), I suggest you make a scheduled process (let's say every 5 min) on the web server. That will get data from the source (http://data.mtgox.com) and put it into database table.
After that you make your own JSON service (an MVC action method) and publish the data from your tables.
It will also allow your customers that your site is working even if mtgox.com is down for some reason.
How to getting Data through web Services with jquery in asp.net c#.
for more details
http://way2finder.blogspot.in/2013/08/how-to-getting-data-through-web.html

Use jquery/ajax to call a c# (or any other .net) function in another project within the same solution

Does anyone know if it's possible to use jquery/ajax to call a c# (or any other .net) function in another project within the same solution?
Let's say that the solution's name is ExampleSolution , the name of the project from which I call the target function is Project.Source and the name of the target project is Project.Target,
and the name of the target function is TargetFunction().
I've tried following in an .js file in the Project.Source:
$.ajax({
url: '/ExampleSolution/Project.Target/TargetFunction',
type: 'get',
success: function (data) {
$(document.body).append(data);
}
});
Then I've modified the url-line in several ways but never succeed.
Do you have any advice?
Thank you all for your fast answers.
I found a solution for the problem and I'd like to share it just in case anybody faces the same problem in the future.
In the .js file before I call the $.ajax function I create a variable with help of window.location which points to the url to the targetfunction of the running target-project and use the variable in the ajax-function. So you don't point to another project. You point to url of running project.
Just as easy as it sounds.
Below follows the solution:
var url = window.location = 'http://localhost:13105/TargetFunction';
$.ajax({
url: url,
type: 'get',
success: function (data) {
$(document.body).append(data);
}
});
});
You can only call functions in the Code Behind because they're being registered by the web server.
If you want to have a function accessible outside the Code Behind it needs to be registered as a ASMX or WCF service.
See Creating and Consuming Your First WCF Service for setting up a WCF Service.
Once it is setup and running you can use Ajax to call the methods just like you would in the Code Behind.
$.ajax({
//Path to WCF Server and Target Method
url: "http://localhost:PORT/wcfsvc/FooService.svc/Foo",
type: 'get',
contentType: 'application/json; charset=utf-8',
success: function (data) {
$(document.body).append(data);
}
});
See also: Consuming WCF from jQuery as JSON
The short answer is "No, it isn't possible." Front-end code (like jQuery and AJAX) run on the client's machine, but C# functions are back-end and run on the server. There's no direct connection between them.
The longer answer is "Not directly, but there are ways to do something similar." Your easiest option is to use AJAX to POST to another controller/action on your server and then process the response. This is close to what you were doing, but you were slightly off. Rather than the url being a function, the url has to be an actual url on your website. Using /Functions/CallTargetFunction as an example, you would then create a controller like this:
public class FunctionsController : Controller
{
public ActionResult CallTargetFunction()
{
return Content(TargetFunction());
}
}
Note that doing this means anyone who visits http://yoursite.com/Functions/CallTargetFunction will get the result of that function.

Categories

Resources