calling asmx service using jquery ajax asp.net 4.0
Asked Answered
S

4

7

I'm trying to call a sample asmx service using jquery, here is the jquery code

$.ajax({
            type: "POST",
            url: "/Services/Tasks.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            contentType: "application/xml; charset=utf-8",
            success: function (data) {                   
                alert(data);                    
            }
        });

This is not showing any message,code is in asp.net 4.0, Am I missing any thing?

Edit - I changed the dataType to xml, now success function is working it return following xml

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>

I'm using following code to parse xml data and it is showing null in alert

success: function (data) {
    edata = $(data).find("string").html();
    alert(data);
}
Surefooted answered 26/1, 2011 at 18:50 Comment(1)
try adding complete: function(x,y,z){ } and see what's returnedRelish
R
6

I believe it's because you have the dataType: "json" and it's expecting the response content-type to be the same but XML is being returned. I bet the complete event is being raised but not success.

try

$.ajax({
            type: "POST",
            url: "/Services/Tasks.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            contentType: "application/xml; charset=utf-8",
            success: function (data) {                   
                alert(data);                    
            },
            complete: function (data) {                   
                alert(data);                    
            }
        });

UPDATE

I think it's because you're using .html(), you need to use text(). Also i don't know if you meant to do it or not but you have data in your alert, i'm assuming you meant to use edata. The following worked for me:

jQuery.ajax({
    type: "POST",
    url: "/yourURL",
    dataType: "xml",
    data: "{}",
    contentType: "application/xml; charset=utf-8",
    success: function(data) {
        edata = $(data).find("string").text();
        alert(edata);
    }
})
Relish answered 26/1, 2011 at 19:0 Comment(4)
u got it, in complete event i'm trying to parse xml as "edata = $(data).find("string").html();" it is returning null.Surefooted
i set datatype as xml, now success works, but now error is in xml parsingSurefooted
you get an error parsing the XML? what are you using to parse the XML? Maybe update your question to include the response XML so we can test it.Relish
Thanks buddy, using text() instead of html resolve the problem.Surefooted
P
2

I'd recommend adding the [ScriptService] attribute to your Tasks.asmx class so it will accept and respond in JSON instead of XML. Your client code looks good, but you'll want to take a look at "data.d" instead of "data" in your success handler.

Phylum answered 27/1, 2011 at 0:22 Comment(0)
Y
2
  use it.

   <script>
        alert("aaa");
    $.ajax({
        type: "POST",
        url: "MyService.asmx/HelloWorld",
        data: "{}",
        dataType: "xml",
        contentType: "application/xml; charset=utf-8",
        success: function (data) {
        alert(data);//data-object xmldocument
        edata = $(data).children("string").text();
        alert(edata);

        }
    });
    alert("bbb");
    </script>
Yurik answered 11/4, 2011 at 15:6 Comment(0)
R
1

Well, you're stating that the dataType is JSON, but the contentType is XML. Try

contentType: "application/json; charset=utf-8",

If not, then we'd have to see the asmx code.

Reluctivity answered 26/1, 2011 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.