Solved

Sample ASMX to return javascript

Posted on 2010-08-27
4
1,438 Views
Last Modified: 2012-05-10
I want to place the following on a number of web pages:
<script type="text/javascript">
src="www.myserver.com/returnjs.asmx"
</script>

I want the asmx (vb.net) to return the following line
document.write('This line was produced by asmx')

0
Comment
Question by:jking1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
MlandaT earned 250 total points
ID: 33546976
There are two seperate issues here:

1. How to invoke an ASMX web service from Javascript
I prefer to use JQuery for that purpose because it helps simplify things.

$.ajax(
    {
        type: "POST",
        url: "ReturnJS.asmx/MyJSMethod",
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        error: OnError
    });

*You'll need to implement the OnSuccess and OnFailure methods. http://www.midnightprogrammer.net/post/Calling-ASPNET-Web-Service-Using-Jquery-Part-I.aspx

2. How to inject javascript into the page
Basically, your web service will need to return a simple string. You will need to execute the javascript returned from teh web service. Supposing that we put the javasccript into a string, you can use the eval() function to execute the javascript.

    var s = "alert('hello')";
    eval(s);

3. Sample web method to return the javascript code:
<WebService(Namespace:="http://tempuri.org/MyService/ReturnJS")> _
Public Class ReturnJS
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function MyJSMethod() As String
        Return "document.write('This line was produced by asmx')"
    End Function

End Class
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33546994
That addresses your question... however.. the eval() function is dangerous as it opens up a can worms as far as security is concerned. Normal practice is to return plain simple text (as opposed to javascript) from the web service and then loading that text into other controls on the page and so on.
0
 

Author Comment

by:jking1234
ID: 33549407
MlandaT,
Thanks for the fast response.
Paragraph 3 above does the Web Services bit.
How should I amend my initial script to execute the returned string ( ie to include the eval() function?

0
 

Author Closing Comment

by:jking1234
ID: 33638862
asdf
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question