ajax failed啥意思,Ajax请求返回200 OK,但会触发错误事件而不是成功

jQuery.ajax attempts to convert the response body depending on the specified dataType parameter or the Content-Type header sent by the server. If the conversion fails (e.g. if the JSON/XML is invalid), the error callback is fired.

Your AJAX code contains:

dataType: "json"

In this case jQuery:

Evaluates the response as JSON and returns a JavaScript object. […]

The JSON data is parsed in a strict manner; any malformed JSON is

rejected and a parse error is thrown. […] an empty response is also

rejected; the server should return a response of null or {} instead.

Your server-side code returns HTML snippet with 200 OK status. jQuery was expecting valid JSON and therefore fires the error callback complaining about parseerror.

The solution is to remove the dataType parameter from your jQuery code and make the server-side code return:

Content-Type: application/javascript

alert("Record Deleted");

But I would rather suggest returning a JSON response and display the message inside the success callback:

Content-Type: application/json

{"message": "Record deleted"}

I've had some good luck with using multiple, space-separated dataTypes (jQuery 1.5+). As in:

$.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

});

You simply have to remove the dataType: "json" in your AJAX call

$.ajax({

type: 'POST',

url: 'Jqueryoperation.aspx?Operation=DeleteRow',

contentType: 'application/json; charset=utf-8',

data: json,

dataType: 'json', //**** REMOVE THIS LINE ****//

cache: false,

success: AjaxSucceeded,

error: AjaxFailed

});

This is just for the record since I bumped into this post when looking for a solution to my problem which was similar to the OP's.

In my case my jQuery Ajax request was prevented from succeeding due to same-origin policy in Chrome. All was resolved when I modified my server (Node.js) to do:

response.writeHead(200,

{

"Content-Type": "application/json",

"Access-Control-Allow-Origin": "http://localhost:8080"

});

It literally cost me an hour of banging my head against the wall. I am feeling stupid...

I reckon your aspx page doesn't return a JSON object.

Your page should do something like this (page_load)

var jSon = new JavaScriptSerializer();

var OutPut = jSon.Serialize();

Response.Write(OutPut);

Also, try to change your AjaxFailed:

function AjaxFailed (XMLHttpRequest, textStatus) {

}

textStatus should give you the type of error you're getting.

I have faced this issue with an updated jQuery library. If the service method is not returning anything it means that the return type is void.

Then in your Ajax call please mention dataType='text'.

It will resolve the problem.

You just have to remove dataType: 'json' from your header if your implemented Web service method is void.

In this case, the Ajax call don't expect to have a JSON return datatype.

Use the following code to ensure the response is in JSON format (PHP version)...

header('Content-Type: application/json');

echo json_encode($return_vars);

exit;

I had the same issue. My problem was my controller was returning a status code instead of JSON. Make sure that your controller returns something like:

public JsonResult ActionName(){

// Your code

return Json(new { });

}

I have the similar problem but when I tried to remove the datatype:'json'

I still have the problem.

My error is executing instead of the Success

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); }

});

}

Another thing that messed things up for me was using localhost instead of 127.0.0.1 or vice versa. Apparently, JavaScript can't handle requests from one to the other.

See this. Its also similar problem. Working i tried.

Dont remove dataType: 'JSON',

Note: echo only JSON Formate in PHP file if you use only php echo your ajax code return 200

I had the same problem. It was because my JSON response contains some special characters and the server file was not encoded with UTF-8, so the Ajax call considered that this was not a valid JSON response.

If you always return JSON from the server (no empty responses), dataType: 'json' should work and contentType is not needed. However make sure the JSON output...

is valid (JSONLint)

is serialized (JSONMinify)

jQuery AJAX will throw a 'parseerror' on valid but unserialized JSON!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值