Solved

Sample ASMX to return javascript

Posted on 2010-08-27
4
1,408 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

New Relic recently released its Synthetics product that allows for the creation of performance monitors that periodically test a site's performance. If you wish to test an interactive workflow New Relic employs Selenium WebDriverJS to run those test…
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 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…

914 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now