开发者

How do I parse-out or access the value returned from jQuery's web-service call?

开发者 https://www.devze.com 2023-02-22 02:57 出处:网络
I am experimenting with calling a web-service using jQuery.For some reason the result is returned as XML...and apart from writing a parser myself...there HAS to be a better way of getting at the resul

I am experimenting with calling a web-service using jQuery. For some reason the result is returned as XML...and apart from writing a parser myself...there HAS to be a better way of getting at the result.

This Is The Value Returned:

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

This Is The HTML:

<script type="text/javascript">

    var url = '<%=ResolveUrl("~/Services/ProjectDialog.asmx/HelloWorld")%>';

    function callWebService() {

        jQuery.ajax({
            cache: false,
            type: 'POST',
            complete: onComplete,
            data: null,
            dataType: 'application/json; charset=utf-8',
            error: onError,
            success: onSuccess,
            url: url
        });
    }

    function onComplete(status, xmlHttpRequest) {
        var stop = "";
    }
    function onError(xmlHttpRequest, status, error) {
        var stop = "";
    }
    function onSuccess(data, status, xmlHttpRequest) {
        var stop = "";
    }

    jQuery(document).ready(function() {
    });

</script>

<input type="button" value开发者_高级运维="Run Web Service" onclick="callWebService();" />

This Is The Web Service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;

namespace My.Services
{
    /// <summary>
    /// Summary description for ProjectDialog
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class ProjectDialog : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}


I can think of three options that would be helpful in this situation -

  1. Include this attribute on the Web Service's Method (to return as JSON instead of XML) -

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

  2. Or Parse the XML on client-side

  3. Or use an IHttpHandler rather than a Web Service. They are easy and more plain to use.


You need to apply the ScriptMethodAttribute to your web service and specify the ResponseFormat (json is the default)

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
    return "Hello World";
}

This will most likely be your best result since it appears you want to request the data as json anyways based upon your .ajax() call. Note the dataType should simply be json based upon the jQuery docs.

Different data handling can be achieved by using the dataType option. Besides plain xml, the dataType can be html, json, jsonp, script, or text.

Update It seems if you specify the contentType correctly you will get a valid json response.

jQuery.ajax({
    cache: false,
    type: 'POST',
    complete: onComplete,
    data: null,
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    error: onError,
    success: onSuccess,
    url: url
});
0

精彩评论

暂无评论...
验证码 换一张
取 消