views:

82

answers:

4

what's my function problem?I wana read from my webservice but I just receive error :(

the browser message is:
"undefined- status:error"

when I press button just I see the error function of my Jqueary calling but I dont know why??plz help me.

function SetupCompanyInfo(companyID) {
    //alert('aaa');
    companyID = '1';
    $.ajax({
        type: "POST",
        url: '../../../Services/CompanyServices.asmx/GetCompanyInfo',
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        error: OnError
    });

}
function OnSuccess(data, status) {
    SetMainBody(data);
}
function OnError(request, status, error) {
    SetMainBody(error + '- ' + request + ' status:' + status);
}

my webservice:

using System;
using System.Web.Services;
using System.Web.Script.Services;


/// <summary>
/// Summary description for CompanyServices
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]

//[System.ComponentModel.ToolboxItem(false)]

public class CompanyServices : System.Web.Services.WebService {

    [WebMethod]
    public string GetCompanyInfo()
    {
        string response = "aaa";
        Console.WriteLine("here"); 
        return response.ToString();
    }

    [WebMethod]
    public string GetCompanyInfo(string id)
    {
        string response = "aaa";
        Console.WriteLine("here2"+id);
        return response.ToString();
    }

}

my aspx file,part of head and my button code:

<script src="../../Scripts/InnerFunctions.js" type="text/javascript"></script>

<script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>

<script src="../../Scripts/TabMenu.js" type="text/javascript"></script>

<script src="Scripts/InternalFunctions.js" type="text/javascript"></script>
     <div dir="rtl" style="border: 1px solid #CCCCCC">
                   <asp:Image ID="Image1" runat="server" ImageUrl="../../generalImg/Icons/64X64/settings_Icon_64.gif" 
                       style="width: 27px; height: 26px" onclick="SetupCompanyInfo(1)" /></div>
A: 

Like you use them your OnError and OnSucces are functions that need parameters. Maybe try something simpler like this first to get more feedback:

error: function(status, error) { alert(status + " - " + error); }
André van Toly
She's using the callbacks perfectly correctly
Adam
A: 

What Content-Type does your webservice respond with? It should be application/json for a JSON response.

CharlesLeaf
A: 

It seems like your webservice only responds with plain text, while the jQuery request expects JSON in return.

JSON is a format that lets you send different datatypes (strings, integers, arrays, etc.) in a coherent manner. Note 18 on this page shows you a typical JSON response for a person listing.

Your response should look something like:

{"companyName": "Foobar Inc."}

And, if I'm not mistaken, your onSuccess function should be something along the lines of:

function OnSuccess(data, status) {
SetMainBody(data.companyName);
}

Now I'm not entirely sure about the jQuery function, but your response is definitely not JSON! :-)

Cip
AFAIK, the default response type of web services is XML, though the rest of your answer is spot-on.
Ryan Kinal
But his jQuery AJAX call is expecting JSON:contentType: "application/json; charset=utf-8",dataType: "json",Bah, cant figure out how to format this comment properly..
Cip
A: 

The address of your asmx service - is that address relative to the javascript file, or to the actual web page you're running? The address should be relative to the web page, or else you can always use a fully qualified url:

http://blah/blah/blah.asmx/serviceMethod

Adam