0

I want to access asp.net web service by only javascript / jquery with out any c# code..
I have generated simple web service like below..

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
[WebMethod]
public string TestNameService(string name)
 {
    return "data-" + name;
 }

On the other domain I am using textbox and button to send request and on label I want to display out put.
Like this

<asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<asp:Button ID="call_service" runat="server" Text="Call Service" OnClientClick="CallService(); return false;" />
<asp:Label ID="lblMsg" runat="server" Text="" />



I had tried with this jquery which is working in same domain.. but i am not able to access method on multiple domains and if i access method then i get only blank response

<script type="text/javascript">
        function CallWebService() {
            $.ajax({
                type: "POST",
                url: "http://......myurl..../WebService.asmx",
                data: "{'name':'" + $("#txt_name").val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: ShowData,
                error: error

            });
        }
        function ShowData(data, status) {
            $("#lblMsg").html(data.d);
        }
        function error(request, status) {  
            alert(request.statusText);
        }
    </script>
9
  • Have you tried with jquery ajax method to call webservice? Commented Feb 5, 2014 at 7:29
  • @mayur yes i have tried but it was not working.. Commented Feb 5, 2014 at 7:30
  • What you are getting error? Commented Feb 5, 2014 at 7:31
  • 3
    @Brad larson - It is not the question which u had edited.. it is also an answer because it is working in same domain. i want to run in multipal domain so i had write in answer so other users can use..who have requirement to run web service through java script in same domain.. if u have answer then say other wise don't edit question because u had changed the direction of question.. Commented Feb 8, 2014 at 5:38
  • 1
    @Brad Larson i don't have any problem. some of users want to know my script for same domain so i have posted as answer to get modified answer for multiple domains. and do you know any thing about this question? u can see the same answer with jquery which was given by mayur borad. that ans is also working for same domain. Brad i m still waiting for answer if u know any answer then give answer except modification of question & comments. i had posted script as answer because i want different ideas from users in place of same script ideas so if possible then keep my question/ans as it was. Thanks. Commented Feb 10, 2014 at 4:18

2 Answers 2

1

If you are using cross domain, you have to specified to jquery with follows

$.support.cors = true;
$.ajax({
                type: "POST",
                url: "http://......myurl..../WebService.asmx",
                data: "{'name':'" + $("#txt_name").val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: ShowData,
                error: error

            });

let me know if not works.

Sign up to request clarification or add additional context in comments.

5 Comments

I have tried this but i m getting same problem at now it is showing --> ReferenceError: CallService is not defined if i add call service in url then it is also giving same problem.. like --> http://......myurl..../WebService.asmx/TestNameService
If you have't 3rd party webservice then put break point on Application_BeginRequest Event of Global.asmx of your webservice project. If you have't this event, create it. and check your debugger point is fired ?
I have run on my local server and i m getting 2 problems in this if i add service name in url then i am getting '500 Internal Server Error - http://---url---/WebService.asmx/TestNameService"' and if i simple run web service then shows - '405 method not allowed web service - http://---url---/WebService.asmx"'
Can you open your webservice? Are u opening it with localhost?
Are getting breakpoint on Global.asmx when you make ajax call?
0

Your normal javascript ajax request will fail because your solutions seems to be on different domains if I understand your question.

You will need to use a technique called CORS, which is supported in the .NET platform as well, see:

http://encosia.com/using-cors-to-access-asp-net-services-across-domains/

3 Comments

thanks for the reply, but as per requirement i have to do it with simple javascript / jquery.. and i have seen this, it is same like my script.. in my script i am not able to access method and if i access method then i get only blank response..
@Shirish With CORS enabled you will be able to do it with javascript only
ok i will try, because i had never worked with this CORS so i have to read all thing.. did u have seen my script? do u have any idea in this script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.