简体   繁体   中英

consuming json with jquery ? .getJSON callback does not fire

I've got a cs webservice that returns this json when passed id: "jdoe"

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">[{"department":"Sales","mail":"jdoe@acme.com"}]</string>

I'm looking for minimal jquery code to display department and am trying this:

$(document).ready(function () {
    url = "http://test.mydomain.com/WebService.asmx/GetDEPT?callback=?"
    alert("start");
    $.getJSON( url, { id: "jdoe" }, function( data ) {  alert(data.department); });
    alert("end");
});

I only see alerts for start and end.

I've checked fiddler and do not see errors as the call is made.

On the local webserver I am prompted for id and it does return the expected json.

My cs looks like this:

    public string GetDEPT(string id)
{
    var json = "";
    var umid  = from result in dc.GET_DEPT(id) select result;

    JavaScriptSerializer jss = new JavaScriptSerializer();

    json = jss.Serialize(id);

    return json;

The problem is that

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">[{"department":"Sales","mail":"jdoe@acme.com"}]</string>

is actually XML with a JSON string inside. For your code to work, you would need to simply pass back:

[{"department":"Sales","mail":"jdoe@acme.com"}]

on its own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM