Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 作为HTML而不是Json接收的ajax Json响应_Javascript_Jquery_Ajax_Json - Fatal编程技术网

Javascript 作为HTML而不是Json接收的ajax Json响应

Javascript 作为HTML而不是Json接收的ajax Json响应,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,我有一个在本地主机上运行良好的应用程序。但一旦我从另一台机器上访问它,它就不能像预期的那样工作 我有以下ajax请求: $.ajax({ type: "POST", dataType: "json", contentType: "application/json; charset=utf-8", url: rootUrl + 'ro/createRO', data: JSON.stringify(postdata), success: functi

我有一个在本地主机上运行良好的应用程序。但一旦我从另一台机器上访问它,它就不能像预期的那样工作

我有以下ajax请求:

$.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);
    }
});
这对成功很有帮助。 但失败时,我得到的xhr.responseText是json格式(这是预期的),但当从另一台机器访问时,它给出了HTML格式。这种行为的可能原因是什么

预期的JSON是

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>
消息:“此操作无法完成,因为它将导致以下估计的预算超调”
它返回的HTML是:
500-内部服务器错误。
服务器错误
500-内部服务器错误。
您正在查找的资源有问题,无法显示。
生成异常的代码

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;
        }
    }
public异步静态任务CreateRO(int[]estItemIds)
{
使用(DDb db=new DDb())
{
RoCreateResult roData=wait db.Database.SqlQuery(
“s_roCreate”)
.FirstOrDefaultAsync();
返回roData;
}
}
我尝试过的事情: 1.尝试使用jsonp而不是json-不起作用。 2.安装了软件包Microsoft.AspNet.WebApi.Cors 5.2.3,并添加了HTTPconfiguration以启用Cors。-失败了


还有什么我可以试试的吗?

返回给您的HTML说

500-内部服务器错误。 您正在查找的资源有问题,无法显示。


这意味着,您试图访问的请求在服务器端出现错误(可能是引发异常的代码)。您需要使用try..catch以服务器端语言包装代码,并将错误记录到db或以json格式返回错误。ie消息:您从代码中捕获的错误。这与您的javascript代码无关。

失败时您会看到什么?我们在这里谈论的是什么样的失败?在“失败”的情况下,服务是否可能没有到达能够返回json的代码层(服务器),但响应是由“默认”处理程序生成的?失败时返回的html tht是什么?您确定响应不是返回的错误页吗?像默认的404页面一样,
rootUrl
包含什么?@GuruprasadRao rootUrl有值“”,如果我从localhost访问它,请删除
rootUrl
,只保留
url:'/ro/createRO'
,如果您从服务器访问它,它会有什么?因为rootUrl和ro/createRo之间没有
/
,所以我尝试使用try-catch。但是数据库引发的异常没有被捕获到catch块中我在您注释公共异步静态任务CreateRO(int[]estItemIds){using(DDb db=new DDb()){RoCreateResult roData=wait db.database.SqlQuery(“s_roCreate”)的代码中没有看到任何try catch.FirstOrDefaultAsync();return roData;}}}在此处或调用此函数的函数内部提供try catch。您将在上述任何一个区域获得它。请允许我解释:假设您是从另一个名为ExecuteCreateRO:public static void ExecuteCreateRO(){try{CreateRO()}catch(Exception ex){//log或return execption message}的函数调用上述方法这将捕获异常。根据它进行操作