Solved

Sample ASMX to return javascript

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

These instructions are based on installing Owncloud on your new raspberry pi connected with a usb HDD. What do you need Part A? A Raspberry Pi, model B. A boot SD card for the Raspberry Pi. A usb HDD An Ethernet cable to connect to the lo…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)

744 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

15 Experts available now in Live!

Get 1:1 Help Now