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
    }
});  
6
  • Are you able to hit the service all the time otherwise if you post nothing (null) to the method? Commented Jan 10, 2011 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. Commented Jan 10, 2011 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? Commented Jan 10, 2011 at 15:16
  • Maybe cache issue.. try to disable cache by adding cache: false to the settings. Commented Jan 10, 2011 at 15:17
  • 1
    @El Ronnoco, see this: 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. Commented Jan 10, 2011 at 15:23

3 Answers 3

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
    }
}); 
Sign up to request clarification or add additional context in comments.

4 Comments

Which library do I need to use JSON.stringify?
json2.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.
I'm afraid I'm forced to develop for IE6. :O
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.
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.