views:

154

answers:

3

I've got a simple $.ajax request that I am trying to fetch some HTML content with in my ASP.Net MVC app.

        // Load the claim table
    function GetClaimTable() {
        $.ajax({
            type: "GET",
            url: "claimtable",
            data: {},
            datafilter: null,
            dataType:'text',
            success: function(msg){
                alert(msg);
                $("#claimTable").html(msg.responseText);
            },
            error: function(msg, sdf, sdd) {
                alert(sdf);
                alert(sdd);
            }
        });

But I am getting a parseerror instead. The call is successful because I see 200 OK in firefox and the error has XmlHttpRequest object which has the correct data in the responseText property.

The code works well in IE but fails in firefox. The url claimtable is a simple MVC Action.

I read here http://stackoverflow.com/questions/655307/jquery-asp-mvc-parsererror-in-ajax-calls that this is due to a typo which was solved in jquery 1.3.2. But I have 1.3.2 and I am getting this error.

Any help?

A: 

no need to do msg.responseText. msg itself is the responseTExt

Funky Dude
Hmm... Well like I said the request is failing. In the error function msg has a responseText property. If it would succeed it wouldn't have it.
Cyril Gupta
A: 

If you are trying to fetch HTML from the server why are you specifying dataType:'text'? What ContentType header your action is sending back? It looks as if there was some inconcistency between the ContentType header the server is sending and the actual content.

Darin Dimitrov
I've tried contentType HTML too. I used text ultimately because that's one contentType that ought not to be parsed.
Cyril Gupta
A: 

I finally found out why this was happening. The reason was ajaxSetup that I had written to process my jQuery webservice requests more smoothly. Apparently something was wrong even though I was overriding the settings in my new function and the parsererror occurred. I deleted ajaxSetup and now things are working well.

This was the function that destroyed 3 hours of my life.

$.ajaxSetup({
    type: "POST",
    cache:false,
    contentType:"application/json;charset=utf-8",
    data:"{}",
    dataFilter: function(data) {
        var msg;

        if (typeof (JSON) !== 'undefined' && 
            typeof (JSON.parse) === 'function')
          msg = JSON.parse(data);
        else
          msg = eval('(' + data + ')');

        if (msg.hasOwnProperty('d'))
          return msg.d;
        else
          return msg;
        }
});

Looks pretty innocent. eh?

Cyril Gupta
It says utf-9...?
Adam Bard
That was a typo, I fixed it.
Cyril Gupta