Ajax请求返回200 OK,但是会引发错误事件,而不是成功

如何解决Ajax请求返回200 OK,但是会引发错误事件,而不是成功

|| 我已经在我的网站上实现了Ajax请求,并且正在从网页调用端点。它总是返回200 OK,但是jQuery执行error事件。我尝试了很多事情,但无法弄清问题所在。我在下面添加我的代码: jQuery代码
var row = \"1\";
var json = \"{\'TwitterId\':\'\" + row + \"\'}\";
$.ajax({
    type: \'POST\',url: \'Jqueryoperation.aspx?Operation=DeleteRow\',contentType: \'application/json; charset=utf-8\',data: json,dataType: \'json\',cache: false,success: AjaxSucceeded,error: AjaxFailed
});
function AjaxSucceeded(result) {
    alert(\"hello\");
    alert(result.d);
}
function AjaxFailed(result) {
    alert(\"hello1\");
    alert(result.status + \' \' + result.statusText);
}
#1ѭ的C#代码
protected void Page_Load(object sender,EventArgs e) {
    test();
}
private void test() {
    Response.Write(\"<script language=\'javascript\'>alert(\'Record Deleted\');</script>\");
}
成功删除后,我需要
(\"Record deleted\")
字符串。我可以删除内容,但是没有收到此消息。这是正确的还是我做错了什么?解决此问题的正确方法是什么?     

解决方法

        
jQuery.ajax
尝试根据指定的
dataType
参数或服务器发送的
Content-Type
标头转换响应主体。如果转换失败(例如JSON / XML无效),则会触发错误回调。 您的AJAX代码包含:
dataType: \"json\"
在这种情况下,jQuery:   将响应评估为JSON并返回一个JavaScript对象。 […]   JSON数据是严格解析的。任何格式错误的JSON是   拒绝并引发解析错误。 […]空的回应也是   拒绝;服务器应返回null或{}的响应。 您的服务器端代码返回状态为“ 8”的HTML代码段。 jQuery期望使用有效的JSON,因此会引发错误回调,提示“ѭ9”。 解决方案是从jQuery代码中删除remove5ѭ参数,并使服务器端代码返回:
Content-Type: application/javascript

alert(\"Record Deleted\");
但我宁愿建议返回JSON响应并在成功回调中显示消息:
Content-Type: application/json

{\"message\": \"Record deleted\"}
    ,        使用多个以空格分隔的
dataType
(jQuery 1.5+),我有些运气。如:
$.ajax({
    type: \'POST\',url: \'Jqueryoperation.aspx?Operation=DeleteRow\',contentType: \'application/json; charset=utf-8\',data: json,dataType: \'text json\',cache: false,success: AjaxSucceeded,error: AjaxFailed
});
    ,        您只需在AJAX调用中删除dataType:\“ json \”
$.ajax({
    type: \'POST\',dataType: \'json\',//**** REMOVE THIS LINE ****//
    cache: false,error: AjaxFailed
});
    ,        这只是出于记录目的,因为我在寻找类似于OP的问题的解决方案时碰到了这篇文章。 就我而言,由于Chrome中的同源策略,我的jQuery Ajax请求被阻止了。当我修改服务器(Node.js)来解决所有问题时:
response.writeHead(200,{
            \"Content-Type\": \"application/json\",\"Access-Control-Allow-Origin\": \"http://localhost:8080\"
        });
从字面上看,我花了一个小时将我的头撞在墙上。我很蠢...     ,        我认为您的aspx页面不返回JSON对象。 您的页面应执行以下操作(page_load)
var jSon = new JavaScriptSerializer();
var OutPut = jSon.Serialize(<your object>);

Response.Write(OutPut);
另外,尝试更改您的AjaxFailed:
function AjaxFailed (XMLHttpRequest,textStatus) {

}
textStatus
应该会给您带来错误的类型。     ,        我已经通过更新的jQuery库面对了这个问题。如果服务方法未返回任何内容,则表示返回类型为
void
。 然后在您的Ajax电话中提及
dataType=\'text\'
。 它将解决问题。     ,        如果您实现的Web服务方法无效,则只需从标题中除去“ 22”。 在这种情况下,Ajax调用不希望具有JSON返回数据类型。     ,        使用以下代码确保响应为JSON格式(PHP版本)...
header(\'Content-Type: application/json\');
echo json_encode($return_vars);
exit;
    ,        我遇到过同样的问题。我的问题是我的控制器返回的是状态码而不是JSON。确保您的控制器返回类似以下内容的内容:
public JsonResult ActionName(){
   // Your code
   return Json(new { });
}
    ,        我有类似的问题,但是当我尝试删除数据类型时:\'json \' 我仍然有问题。 我的错误正在执行而不是成功
function cmd(){
    var data = JSON.stringify(display1());
    $.ajax({
        type: \'POST\',url: \'/cmd\',contentType:\'application/json; charset=utf-8\',//dataType:\"json\",data: data,success: function(res){
                  console.log(\'Success in running run_id ajax\')
                  //$.ajax({
                   //   type: \"GET\",//   url: \"/runid\",//   contentType:\"application/json; charset=utf-8\",//   dataType:\"json\",//   data: data,//  success:function display_runid(){}
                  // });
        },error: function(req,err){ console.log(\'my message: \' + err); }
    });
}
    ,        令我感到困惑的另一件事是使用
localhost
而不是127.0.0.1,反之亦然。显然,JavaScript无法处理彼此之间的请求。     ,        看到这个。它也有类似的问题。我试过的工作。 不要删除
dataType: \'JSON\',
注意:如果仅使用php,则仅在PHP文件中回显JSON Formate。echo ajax code return 200     ,        我有同样的问题。这是因为我的JSON响应包含一些特殊字符,并且服务器文件未使用UTF-8编码,因此Ajax调用认为这不是有效的JSON响应。     ,        如果您始终从服务器返回JSON(无空响应),则应该使用
dataType: \'json\'
,而无需
contentType
。但是请确保JSON输出... 有效(JSONLint) 已序列化(JSONMinify) jQuery AJAX将在有效但未序列化的JSON上抛出\'parseerror \'!     ,        您的脚本要求返回JSON数据类型。 尝试这个:
private string test() {
  JavaScriptSerializer js = new JavaScriptSerializer();
 return js.Serialize(\"hello world\");
}
    ,        尝试跟随
$.ajax({
    type: \'POST\',data: { \"Operation\" : \"DeleteRow\",\"TwitterId\" : 1 },error: AjaxFailed
});
要么
$.ajax({
    type: \'POST\',url: \'Jqueryoperation.aspx?Operation=DeleteRow&TwitterId=1\',error: AjaxFailed
});
在JSON对象中使用双引号而不是单引号。我认为这将解决问题。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-