Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sample ASMX to return javascript

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

Threat Trends for MSPs to Watch

See the findings.
Despite its humble beginnings, phishing has come a long way since those first crudely constructed emails. Today, phishing sites can appear and disappear in the length of a coffee break, and it takes more than a little know-how to keep your clients secure.

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

650 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