Most likely, since it is working sometimes, you probably are sending or receiving too much data. You will need to make adjustments on both your server-side and your client-side configs to change the maximum payload that HTTP will send and/or receive. This would be the place I would look first, just because it can be a quick thing to test. However, it isn't the only possibility.
There are a number of questions like this on the web, and there are quite a few with some really good advice. Here are a couple I found: