1

The following code returns a blank response no matter whether or not the Function exists, or even the Web Service file entirely:

$.ajax({
    url: "/ws.asmx/HelloWorld"
    , type: "POST"
    , contentType: 'application/json; charset=utf-8'
    , data: '{ FileName: "' + filename + '" }'
    , dataType: 'json'
    , success: function (data) {

    }
});

Why is this?

Also, might be worth noting, $.load() works fine!

Curtis
  • 101,612
  • 66
  • 270
  • 352

7 Answers7

4

Your error is that you try to construct JSON data manually and do this in the wrong way:

'{ FileName: "' + filename + '" }'

You should fix the code at least to the following

'{ "FileName": "' + filename + '" }'

because correspond to the JSON specification the property names must be also double quoted.

Next problem can you has if the filename has some special characters. For example, in case of

var filename = '"C:\\Program Files"'; // the '\' must be escaped in the string literal

you should has as the data the corresponding JSON string

'{ "FileName": "\\"C:\\\\Program Files\\"" }'

as the corresponding JSON data because of '\' and '"' must be escaped. It looks dificult. So I stricly recommend you to construct JSON strings with respect of JSON.stringify function from the json2.js. Then the code will be

$.ajax({
    type: "POST",
    url: "ws.asmx/HelloWorld",
    data: JSON.stringify({ FileName: filename }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        alert(data.d);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Error Occured!" + " | " + XMLHttpRequest.responseText +
               " | " + textStatus + " | " +  errorThrown);
    }
});

which is simple and clear enough. The next advantage of the usage JSON.stringify is that the most modern web browsers has native support of the function and the function work very quickly.

By the way in case of the usage of JSON.stringify you can easy call web service methd having very complex data structures (classes) as the parameters and not only strings.

UPDATED: One more remrk to reduce possible misunderstanding. It you later deceide to use HTTP GET instead of HTTP POST to call the web method you will have to change the data parameter from

JSON.stringify({ FileName: filename })

to

{ FileName: JSON.stringify(filename) }

UPDATED 2: You can download this Visual Studio 2010 project which I used to test all before I posted my answer. I included as "Web-3.5.config" the web.config for .NET 3.5. All different commented data values included in the default.htm work. If you want make tests with HTTP GET you should uncomment the section in web.config which allows HttpGet and use ScriptMethod having UseHttpGet = true. All the lines are included in the demo as comments.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks for your reply @Oleg, but I'm not sure this is the cause of the issue. The FileName is literally a string of alphanumeric characters, and when viewing the POST request in FireFox FireBug it shows a POST request, with the correct JSON data being sent. – Curtis Feb 28 '11 at 09:11
  • @Curt: Which data exactly you see posted? You can vertfy the string in http://www.jsonlint.com/. The string like `{FileName: "bla"}` or `{'FileName': 'bla'}` are **NOT** valid JSON. The string `{"FileName": "bla"}` is correct. You confuse the JSON-like JavaScript syntax with the data-interchange format JSON described [here](http://www.json.org/). ASMX web service accept only correct JSON data. – Oleg Feb 28 '11 at 09:49
  • @Curt: Do you tried my suggestions? I did it before writing my answer. If it help you I could upload the test VS2010 project C# which I used and post URL to it. Or do you have no interest in the problem? – Oleg Feb 28 '11 at 20:56
  • Thanks for your help with this @Oleg. I'm very interested in resolving this problem and have spent many hours working on doing so. I'm quite certain my JSON format is correct as it comes out [code]{"FileName" : "FileName-Here"}[/code]. Also, I have a similar project to this which works fine! Could it be anything to do with the .NET application? Perhaps a setting which is different in the web.config? Your helps much appreciated – Curtis Mar 01 '11 at 09:11
  • @Curt: Which version of Visual Studio you use (VS2010, VS2008), which language (C#/VB) and which version of .NET is your destination (.NET 4.0/3.5)? If you suspect problems in your web.config you can include it in the body of your question. – Oleg Mar 01 '11 at 09:16
  • @Curt: I appended my answer with [the url](http://www.ok-soft-gmbh.com/jQuery/AsmxWebService.zip) to the demo project which I used. – Oleg Mar 01 '11 at 09:48
  • Thanks for your example @Oleg, it's been very helpful, however it doesnt seem to have changed anything. What I don't understand is that in my script the only function I have is 'HelloWorld'. If I put '/ws.asmx/Lorem' surely this should return a 'Web Service not found' error message, but instead its just a blank response? Your help is very much appreciated as always! – Curtis Mar 01 '11 at 11:24
  • @Curt: If you change 'ws.asmx/HelloWorld' in my example to 'ws.asmx/Lorem' you will receive the error "... Unknown web method Lorem ...". If you change 'ws.asmx/HelloWorld' to 'blabla.asmx/HelloWorld' you will receive the error "... No web service found at /blabla.asmx/HelloWorld ...". – Oleg Mar 01 '11 at 11:34
  • This is strange... I can't find any difference with the applications :/ FYI: $.load works fine.. – Curtis Mar 02 '11 at 13:08
  • @Curt: You can upload the project somewhere and post the url to it like I did. The problem which you have is sure not in the parts of code which you included in your question. – Oleg Mar 02 '11 at 13:16
2

just to try use:

$.getJSON("/ws.asmx/HelloWorld", function(data){
   alert(data);
});

Check if you get the data back.

alexl
  • 6,841
  • 3
  • 24
  • 29
1

Use AJAX-Enabled Web Service

Yuriy Naydenov
  • 1,875
  • 1
  • 13
  • 31
0

Make sure you have loaded the jquery.js file properly.

steve
  • 849
  • 2
  • 9
  • 15
  • Previous to the coded provided are other jquery actions so I'm certain this is working fine – Curtis Feb 25 '11 at 17:01
0

Does the service return a value? If not, it will just POST and not give you back anything, because there is no data to see...

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
  • The service is supposed to return the string "Hello World", but it doesn't. Also, like stated, if I change the service to something like "HelloWorld123" no error is returned. I'm used to seeing a .NET error returned stating "Web Service cannot be found" or something similar. – Curtis Feb 25 '11 at 17:00
0

If you want to watch for errors, you can add an error callback

$.ajax({
    url: "/ws.asmx/HelloWorld"
    , type: "POST"
    , contentType: 'application/json; charset=utf-8'
    , data: '{ FileName: "' + filename + '" }'
    , dataType: 'json'
    , success: function (data) {

    }
    , error: function (a, b, c) {

    }
});

From jQuery:

error(jqXHR, textStatus, errorThrown)Function 

A function to be called if the request fails. The function receives

three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". This is an Ajax Event. As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests.

ihamlin
  • 180
  • 3
  • I tried putting an alert in the error() function, but the alert didn't appear, therefore I assume this was a success response, but blank! – Curtis Feb 28 '11 at 12:01
0

I read somewhere that the contentType header for POST must be:

  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

and I use it blindly: it's always worked.

-- pete

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51