1

I have a jQuery.ajax call on a button click event in my webpage. This ajax call sends quite a lot of markup back to the server. After some processing the server posts back a small URL address. This works fine sometimes but other times doesn't. I have a breakpoint just before the ajax call and also have some in my WebMethod. It appears that sometimes the WebMethod doesn't even get hit.

What could be causing the .ajax call to fail? I'm assuming there must be something in the parameters I am sending. But I am escapeing the markup.

Anyone got any ideas?

$.ajax({
    type: 'POST',
    url: 'WebServices.asmx/GetBitmapPathForVML',
    contentType: 'application/json; charset=utf-8',
    data: '{"sVML" : "' + escape($('#divChart')[0].innerHTML) + 
      '","width" : 800,"height": 600}',
    dataType: 'json',
    success: function(result) {
            var newWindow = window.open ("", "Chart","");
            //blah blah
            newWindow.document.write("<BODY>");
            newWindow.document.write(
              '<img src="file" alt="Chart"></img>'.replace('file',result.d)
            );  
            newWindow.document.write("</BODY>");    
            //blah blah
    }
});  
John Saunders
  • 160,644
  • 26
  • 247
  • 397
El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
  • Are you able to hit the service all the time otherwise if you post nothing (null) to the method? – dexter Jan 10 '11 at 15:06
  • 1
    Check "error" and "timeout" options for jQuery ajax, that should help you. Also if there's some problem with the way you're calling the webservice, you should get a script error from the client. – Jamie Treworgy Jan 10 '11 at 15:07
  • @jamietre Thanks, I caught the error. *The length of the string exceeds the value set on the maxJsonLength property* Any idea how I can handle this? – El Ronnoco Jan 10 '11 at 15:16
  • Maybe cache issue.. try to disable cache by adding `cache: false` to the settings. – Shadow The GPT Wizard Jan 10 '11 at 15:17
  • 1
    @El Ronnoco, see this: http://stackoverflow.com/questions/1151987/unlimited-for-maxjsonlength ... however rather than trying to adjust the max allowed size you should think about the architecture, why are you sending a whole HTML table back to the client? Probably you should extract just the relevant data and send that. That should be pretty easy with jquery. If you really want to do this for some reason you might have to break it into chunks and call the service multiple times. – Jamie Treworgy Jan 10 '11 at 15:23
  • Here's another thought. Since it seems like your server code is parsing a bunch of HTML markup from some web page into something else, could you just send it the URL of the page you're on, and have it read it directly from the server and parse it? E.g. instead of "pushing" a whole web page to the server, just let it "pull" it directly. – Jamie Treworgy Jan 10 '11 at 15:32

3 Answers3

2

I would recommend you rewriting your method like this:

$.ajax({
    type: 'POST',
    url: 'WebServices.asmx/GetBitmapPathForVML',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({
        sVML: $('#divChart').html(),
        width: 800,
        height: 600
    }),
    dataType: 'json',
    success: function(result) {
        var newWindow = window.open ("", "Chart","");
        //blah blah
        newWindow.document.write("<BODY>");
        newWindow.document.write(
            '<img src="file" alt="Chart"></img>'.replace('file',result.d)
        );  
        newWindow.document.write("</BODY>");    
        //blah blah
    }
}); 
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Which library do I need to use `JSON.stringify`? – El Ronnoco Jan 10 '11 at 15:11
  • [`json2.js`](https://github.com/douglascrockford/JSON-js). But this method is natively implemented in modern browsers and the library simply delegates the call to the native method if supported. So if you are using a modern browser you don't need to include anything. – Darin Dimitrov Jan 10 '11 at 15:14
  • I'm afraid I'm forced to develop for IE6. :O – El Ronnoco Jan 10 '11 at 15:26
  • This is good advice as I wasn't happy with my JSON construction. I will endeavour to use this in future projects however in this instance this was not the cause of my problem. – El Ronnoco Jan 10 '11 at 15:27
1

El Ronnoco,

I would suggest you to add a error: callback to check what is going on. Maybe you can get usefull information from that.

1

Don't like answering my own question (not that I am, really). But the issue was to do with the maximum JSON length property.

I found the answer here

..and added this to my webconfig...

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="2097152"/>
        </webServices>
    </scripting>
</system.web.extensions>    

Thanks for all the answers guys, especially those about catching the errors.

Community
  • 1
  • 1
El Ronnoco
  • 11,753
  • 5
  • 38
  • 65