Title: Problem reading webservice with jquery?
Tags: c#;javascript;jquery
Question: 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&gt;
/// Summary description for CompanyServices
/// </summary&gt;
[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"&gt;</script&gt;
<script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"&gt;</script&gt;
<script src="../../Scripts/TabMenu.js" type="text/javascript"&gt;</script&gt;
<script src="Scripts/InternalFunctions.js" type="text/javascript"&gt;</script&gt;
<div dir="rtl" style="border: 1px solid #CCCCCC"&gt;
<asp:Image ID="Image1" runat="server" ImageUrl="../../generalImg/Icons/64X64/settings_Icon_64.gif"
style="width: 27px; height: 26px" onclick="SetupCompanyInfo(1)" /&gt;</div&gt;
Comment: I try my address with command prompt...
I was correct!

Comment: Is the url you entered visible to the browser? If not then that could be the problem

Comment: try to check if the ajax function was able to connect to websevice(asmx).  via "Fiddler2"  http://www.fiddler2.com/

Comment: The ajax url is unclear..   try write  full url  starting with "http://"

Here is the accepted answer: 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 + "'}",
Comment for this answer: @rima, the "http://www.hiradsign.com/Services/CompanyServices.asmx?op=GetCompanyInfo" URL looks correct. Your function does expect an argument, so your `data: "{}"` needs to be changed to something like `data: "{'id':'test'}" `.
Comment for this answer: @rima, sorry, I pasted the wrong URL - your URL above is along the right lines `http://www.hiradsign.com/Services/CompanyServices.asmx/GetCompanyInfo`.
Comment for this answer: @rima, It's got a learning curve, but it'll be better once you get a handle on the basics :) You definitely do not need to pass any parameters in the query string (i.e. `?id=something&test=name` is not necessary).
Comment for this answer: @rima Try changing your success function to `alert(data.d);` just to verify that you're getting the data you expect in the response. If so, then the problem lies in your `SetMainBody` function.
Comment for this answer: @rima Also, your `data` parameter still looks incorrect in the SetupCompanyInfo function in your InternalFunctions.js file and the wagme function is referencing a nonexistant error handler `err`.
Comment for this answer: Please help me, I am try to debug it, do I need to change the URL? I mean I dont need add my function name at the end of my URL?
Comment for this answer: So we need to add our function at the end of our URL,previous time I didnt use,let me test.. sorry I bother u tonite ;)
Comment for this answer: @jball I dont need add & ??? sorry It is my first time using Jquery,very bad experience
Comment for this answer: @jball : still I have problem jball :(( check http://www.hiradsign.com/seller/sellermanager/
Comment for this answer: @jball: check http://www.hiradsign.com/seller/sellermanager/  I think it worked,but how I can see my text?
Comment for this answer: @jball: THnak u sooooooooooooooooooooooooooooo muccccccccccccccccchhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh :*) HUG

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

Here is another answer: 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); }

Here is another answer: 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! :-)
Comment for this answer: Cip would u tell me do I need customize my URL?Do I need put my webservice function at the end of my webservice?
Comment for this answer: 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..
Comment for this answer: AFAIK, the default response type of web services is XML, though the rest of your answer is spot-on.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:05/31/10', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c#><javascript><jquery>', 'Title': 'Problem reading webservice with jquery?', 'ViewCount': 488, 'id': 2943964, 'user_AboutMe': '<p>I am a hardworking and motivated software engineer who has acquired extensive knowledge in the field through the study of software engineering at the PhD level. I have working experience in the information technology and financial industry in the insurance sector. I am looking for a new challenge on my career path to fulfil my experience in a different career path with new opportunities. Moreover, I am keen to learn new technologies and I am a victim of developaralysis...</p>\n', 'user_AccountId': 138066.0, 'user_DisplayName': 'Amir', 'user_Reputation': 1823.0, 'user_Views': 721.0, 'user_id': 342743.0}}