简体   繁体   中英

ajax Json response received as HTML instead of json

I have an application which works fine on my localhost. But as soon as i access it from a different machine, it doesnt work as expected.

I have a following ajax request:

$.ajax({
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    url: rootUrl + 'ro/createRO',
    data: JSON.stringify(postdata),
    success: function (RoCreateResult) {
        /
        if (allRowsSelected) {
            window.location = rootUrl + "ro/RoPendingSummary?RoNumber=" + RoCreateResult.roNumber;
        } else {
            _AE.successAlert("RO " + RoCreateResult.roNumber + " created successfully");
            refreshRoPendingGrid();
        }
    },
    error: function (xhr, status, error) {

        var errobj = eval("(" + xhr.responseText + ")");
        _AE.errorAlert(errobj.message);
    }
});

This is working fine on success. But on failure, xhr.responseText that i get is in json format(which is expected) but when accessed from another machine, it gives a HTML format. What could be the possible reason for this behaviour?

The Expected JSON is

message:"This operation cannot be completed because it will result in budget overshoot for the following estimates"

The HTML that it returns is:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0       Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-    family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-        top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>500 - Internal server error.</h2>
  <h3>There is a problem with the resource you are looking for, and it     cannot be displayed.</h3>
 </fieldset></div>
</div>
</body>
</html>

The Code which generates exception

public async static Task<RoCreateResult> CreateRO(int[] estItemIds)
    {

        using (DDb db = new DDb())
        {
            RoCreateResult roData = await db.Database.SqlQuery<RoCreateResult>(
                "s_roCreate")
                .FirstOrDefaultAsync();

            return roData;
        }
    }

Things i tried: 1. Tried to use jsonp instead of json - Didnt work. 2. Installed the package Microsoft.AspNet.WebApi.Cors 5.2.3 and added the HTTPconfiguration to enable CORS. - Failed.

Is there anything else which I can try?

The HTML that returned to you says

500 - Internal server error.

There is a problem with the resource you are looking for, and it cannot be displayed.

which means, there is an error on the server side for the request that you are trying to access (could be a code that is throwing exception). you need to wrap the code in your server side language using try..catch and log the error to db or return the error in your json format. ie message : the error that you have catched from your code. this is not related to your javascript code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM