English 中文(简体)
我怎么能说明,成功获得的1.ajax() 是html还是json?
原标题:How can I tell whether the returned data of a successful $.ajax() is of html or json?
  • 时间:2012-04-16 17:50:51
  •  标签:
  • jquery

I have a form that gets submitted to the server via jQuery .ajax() POST. If the form passed the validation on the server-side, the server would return result in HTML for the client-end to update its presentation accordingly. If, however, the form failed the validation, the server would return result in JSON, which consists of the validation errors.

两种结果将在<代码>>上填入.ajax()。 由于这两种类型都是可能的,手工艺者需要一种方法来确定其结果是否为超文本或初才。 我如何能够这样做?

Note: On the surface, my question looks like the same as this existing SO question but they are not the same. In that question, there s only one possible datatype (HTML or JSON), while my problem is about finding a way to deal with two possible datatypes (HTML and JSON).

最佳回答

For json data, typeof(data) will be object. For html data it will be string.

At least with data returned from ASP.NET MVC3 actions it works. I assume that it is the mime type that decides how jquery handles the returned data.

问题回答

如果你离开<代码>数据 类型参数空白,j Query将根据监测和评价系统类型确定:

dataTypeString

Default: Guelliess (xml, json, script, or html)

您从服务器中回收的数据类型。 如果未具体说明,则j Query将试图根据监评小组的答复类型加以推断。

参看:

为什么不只利用JSON返回超文本?

我通常做的是确定我返回的逊尼派目标,如:

{
  //s=status, d=data
  "s":0, //0 means success, other numbers are for different errors
  "d":{ /* Other JSON object or string here */ }
}

因此,就你而言,你就是这样做的:

if (StuffIsValid()) {
    ResponseWrite( {"s":0,"d":"<html>html code here</html>"} );
} else {
    ResponseWrite( {"s":1,"d":{"errlist":["err1","err2"]}} );
}

当然,你希望利用已建的JSON图书馆作为你选择的服务器边语言,而不是使用插座。

然后,在您的格文中success。 背靠背,我将检查点值。

$.ajax({
    url:  url ,
    dataType:  json ,
    success: function(data) {
        if (data) {
            //We have a JSON object
            if (data.s === 0) {
                //Success!
                //Do stuff with data.d as a string
            } else if (data.s === 1) {
                //Failed validation
                //Do stuff with data.d as an object
            } else {
                //How did this happen?
            }
        } else {
            //Uh oh, no object, user must have been logged out (or something)
        }
    });

如果用户必须登上你重新登出的网页,这尤其有用,因为你可以发现,所交数据为JSON物体。





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签