?
Solved

Sample ASMX to return javascript

Posted on 2010-08-27
4
Medium Priority
?
1,451 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 31

Accepted Solution

by:
MlandaT earned 1000 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 31

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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
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)
Suggested Courses

771 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