Amir
Amir

Reputation: 1979

Problem reading webservice with jquery?

What's my function's problem? I want to read the response from my webservice but I just receive an error.

The browser message is:

undefined- status:error

when I press the button I just see the error function of my jQuery call but I don't know why. Please 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>

Upvotes: 0

Views: 500

Answers (4)

jball
jball

Reputation: 25014

Cip was on the right path - you need to make the response JSON, and then properly access the JSON on the client side. Your ASMX method should look like this:

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)] // <--- To make it JSON
public string GetCompanyInfo(string id)     
{         
    Console.WriteLine("here2"+id);         
    return "aaa"; //never call "ToString()" on a string...
} 

And your client side JS success function should access the data like this (you need to access the d property for JSON generated by ASP.Net):

function OnSuccess(data, status) { 
     SetMainBody(data.d);  //"d" is the js object representation of the return 
                           //value from the web method. In your case it's just
                           //a string, but it could be a more complex object 
                           //or an array
}  

When you call the method via ajax, you should pass in the right arguments as well. Something like:

data: "{'id':'" + companyID + "'}", 

Upvotes: 2

Sleavely
Sleavely

Reputation: 1683

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! :-)

Upvotes: 1

CharlesLeaf
CharlesLeaf

Reputation: 3201

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

Upvotes: 0

Andr&#233; van Toly
Andr&#233; van Toly

Reputation: 565

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); }

Upvotes: -2

Related Questions