AJax¼¼Êõ

ÔÙ̸Jquery Ajax·½·¨´«µÝµ½action

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2016-08-13 13:00 ÎÒÒªÆÀÂÛ( )

֮ǰд¹ýһƪÎÄÕÂJqueryAjax·½·¨´«Öµµ½action£¬±¾ÎÄÊǶԸÃÎĵIJ¹³ä¡£¼ÙÉècontrollerÖеķ½·¨ÊÇÈçÏ£ºpublicActionResultReadPerson(PersonModelmodel)&nb..

²©Ö÷µÄ¸ü¶àÎÄÕÂ>>

ÔÙ̸Jquery Ajax·½·¨´«µÝµ½action

2012-09-07 14:15:03

±êÇ©£ºMVC

Ô­´´×÷Æ·£¬ÔÊÐíתÔØ£¬×ªÔØʱÇëÎñ±ØÒÔ³¬Á´½ÓÐÎʽ±êÃ÷ÎÄÕ ԭʼ³ö´¦ ¡¢×÷ÕßÐÅÏ¢ºÍ±¾ÉùÃ÷¡£·ñÔò½«×·¾¿·¨ÂÉÔðÈΡ£

֮ǰд¹ýһƪÎÄÕÂJquery Ajax·½·¨´«Öµµ½action£¬±¾ÎÄÊǶԸÃÎĵIJ¹³ä¡£

¼ÙÉè controllerÖеķ½·¨ÊÇÈçÏ£º 

 ÆäÖÐPersonModel¶¨ÒåÈçÏ£º 

ÄÇôcontroller·½·¨·Ö±ð½ÓÊܵ¥¸ömodelºÍÒ»¸ömodelµÄList¡£²ÉÓÃͨ¹ýajax´«µÝ²ÎÊý¡£

¶ÔÓÚ´«µÝµ¥¸ö²ÎÊýµÄÇé¿ö,¼ÙÉèjs´úÂëÈçÏ£º 

 ´ÓchromeÖнØͼ¿ÉÒÔ¿´µ½ÈçÏ£º

clipboard_thumb

´«µÝµÄÊý¾ÝÊÇÒ»´®FormÊý¾Ý£¬¸ù¾ÝÃüÃûÆ¥ÅäµÄÔ­Ôò£¬Ò²ÊÇ¿ÉÒÔÈ¡µÃÊý¾ÝµÄ¡£

image_thumb

½«option µÄ´úÂë¸Ä³ÉÈçÏ 

ÆäÖÐJSON.stringify·½·¨Ç©ÃûΪ stringify ( value [ , replacer [ , space ] ] )£¬¸ù¾ÝECMA-262±ê×¼stringify º¯Êý·µ»ØµÄÊÇJSON¸ñʽµÄ×Ö·û´®¡£Ëü¿ÉÒÔÓÐ3¸ö²ÎÊý¡£Õª³­ÈçÏ£º

The stringify function returns a String in JSON format representing an ECMAScript value. It can take three parameters. The first parameter is required. The value parameter is an ECMAScript value, which is usually an object or array, although it can also be a String, Boolean, Number or null. The optional replacer parameter is either a function that alters the way objects and arrays are stringified, or an array of Strings and Numbers that acts as a white list for selecting the object properties that will be stringified. The optional space parameter is a String or Number that allows the result to have white space injected into it to improve human readability.

ĬÈϵÄContentTypeµÄÊôÐÔÖµÊÇ"application/x-www-form-urlencoded"

Òý×Ô

¿´ÇëÇóÍ·µÄ½Øͼ£º

clipboard[4]_thumb

Òò´Ë£¬´«µÝµ½controllerµÄÊÇÒ»¸öjson×Ö·û´®£¬MVC¸ù¾ÝÃüÃûÆ¥ÅäÒ²ÊÇ¿ÉÒÔ»ñµÃµ½²ÎÊýµÄÖµ¡£

½«½«option µÄ´úÂë¸Ä³ÉÈçÏÂ

°ÑcontentType¸Ä³Éjson¸ñʽ£¬ÄÇôµÃµ½µÄÊdzö´íµÄÐÅÏ¢¡£

ËäÈ»personÊÇjson¶ÔÏ󣬵«ÊÇjqueryÖеÄajax£¬data»á×Ô¶¯µÄ±»×ª»»³É²éѯ×Ö·û´®¸ñʽkey1=value1&key2=value2ÕâÖÖÐÎʽ£¬ºÜÏÔÈ»ÕâÖÖÐÎʽ²»ÊÇjson¸ñʽ£¬Òò´Ë»á³ö´í¡£

Òª±ÜÃâת»»³É²éѯ×Ö·û´®¸ñʽ£¬Ö»ÐèÒªÉèÖÃprocessDataΪfasle¼´¿É¡£processDataĬÈÏÊÇtrue¡£

ÕâÀïÐèҪעÒâµÄÊÇ£ºµ±Ö¸¶¨ÁËcontentTypeµÄʱºò£¬Êý¾Ý½«²»ÔÙ°´ÕÕForm DataµÄÐÎʽÌá½»ÁË£¬¶øÊDZä³ÉRequest DataµÄÐÎʽÌá½»¡£¿ÉÒÔ´ÓͼÉϵÄRequest HeaderÖп´³ö¡£ÐèҪעÒâµÄÊÇ£¬Form DataÌá½»µÄÊý¾Ý¿ÉÒÔÓÉFormCollection»ñµÃµ½¡£Request Data·½Ê½Ìá½»µÄÔò²»ÄÜͨ¹ýFormCollection»ñµÃ¡£

Èç¹û°ÑprocessDataÉèÖÃΪĬÈÏÖµtrue¡£

image_thumb[3]

Èç¹û°ÑprocessDataÉèÖÃΪfalse¡£

image_thumb[2]

ÒÔÉÏÁ½ÖÖ·½Ê½£¬°´ÕÕapplication/jsonµÄÀàÐÍ´«¸ø¶¼»áʧ°Ü£¬ÒòΪjsonÊÇ»ùÓÚÎı¾µÄ¸ñʽ£¬ÉÏÃæÁ½ÖÖ·½Ê½´«µÝµÄ¶¼²»ÊÇjsonÎı¾¡£Òò´Ë»á³ö´í¡£

Òò´Ë£¬°Ñoption¸Ä³É£º

Ôò´«µÝµÄ¾ÍÊÇjsonÎı¾£¬Òò´Ë¸ù¾ÝÃüÃûÆ¥Å䣬¾ÍÄÜ»ñµÃÖµÁË¡£

clipboard[8]_thumb

¶ÔÓÚ½ÏΪ¼òµ¥ÊÇÊý¾ÝÀàÐÍ£¬ÓÐʱºò²»Ö¸¶¨contentTypeÒ²ÄÜͨ¹ýÃüÃûÆ¥Åä´«Öµ¡£µ«ÊǶÔÓÚÉÔ΢¸´ÔÓµãµÄÊý¾ÝÀàÐÍ£¬ÓÐʱָ¶¨contentType: 'application/json',´¦ÀíÆðÀ´¸ü¼Ó·½±ã¡£

Èç¹ûÒ»¸öcontrollerÀïµÄaction·½·¨ÊǽÓÊÜÒ»¸öListÀàÐ͵IJÎÊý£¬±ÈÈ磺

public ActionResult ReadPersons(List<PersonModel> model)

ÄÇôjsÖÐÏȹ¹ÔìÕâÑùµÄÒ»¸öjson¶ÔÏóµÄÊý×é¡£ÈçÏÂ

µ¥´¿Ò»¸öÊý×é´«µÝÊÇ×÷Ϊdata´«µÝÊÇ£¬Form DataÒ²ÊÇÎÞ·¨Ê¶±ð³öµÄ¡£Òò´Ë°ÑÕâ¸öÊý×éÔÙ´Î×é³ÉÒ»¸öjsonÐÎʽ¡£ÈçÏ£ºÆäÖÐjsonµÄ¼üÖµÓÃmodelÊÇΪÁËÄܺÍcontrollerÖеIJÎÊýÃûÏàͬ£¬¿ÉÒÔÆ¥Åä¡£ 

ÓÉÓÚδָ¶¨contentType£¬Òò´ËÊÇĬÈϵÄapplication/x-www-form-urlencoded¡£´ËʱÊÇ°´ÕÕForm DataµÄ·½Ê½´«µÝµÄ£¬

clipboard

¿ÉÒÔ´Ó½ØͼÖп´µ½¡£µ«ÊÇÕâÖÖ¸ñʽµÄÊý¾Ý£¬controllerÖÐÖ»ÄÜ»ñµÃÖ¸¶¨modelÓÃ2¸öÔªËØ£¬ÎÞ·¨»ñµÃÔªËØÖÐÊôÐÔµÄÖµ¡£

clipboard[1]

Èç¹û°Ñdata¸Ä³ÉJSON.stringify(jsonp)£¬ÈçÏ£º     

clipboard[2]

ÄÇô´«µÝ¹ýÈ¥µÄForm DataÊÇÒ»´®×Ö·û´®£¬controller¸úÎÞ·¨Ê¶±ð³öÕâ¸ö¶«Î÷£¬Òò´Ë»ñ²»µ½Öµ¡£Èç¹û½ö½öÉèÖÃcontentType: 'application/json',¶ø´«µÝµÄÓÖ²»ÊÇjson¸ñʽµÄÊý¾Ý£¬ÈçÏ£º

ÒòΪjqueryµÄajax·½·¨»á°Ñdataת»»³É²éѯ×Ö·û´®£¬Òò´Ë¾Í±ä³ÉÈçϵÄÑù×Ó¡£Õâ´®Îı¾µ±È»²»·ûºÏjson¸ñʽ£¬Òò´Ë»á³öÏÖÏÂÃæµÄ´íÎó¡£

clipboard[4]

Èç¹ûÉèÖÃcontentType: 'application/json',²¢ÇÒÉèÖÃdata: JSON.stringify(persons)£¬ÈçÏ£º

ÄÇô¿ÉÒÔ»ñµÃµ½ÕæÕýÍêÕûµÄjsonÊý¾ÝÁË

clipboard[5]

×îºó£¬´Ë´¦ÔÙÑÝʾһ¸ö¸ü¸´ÔӵIJÎÊýÀàÐÍ£¬ÒÔ±ã¼ÓÉîÀí½â¡£

Ê×ÏÈ¿´Ò»ÏÂControllerÖеķ½·¨Ç©Ãû£¬TestClassB ºÍÒ»¸öTestClassAµÄList¡£ÉÔÏÔ¸´ÔÓ¡£ 

ÔÙ¿´TestClassAºÍTestClassB£¬¸üÏÔ¸´ÔÓ¡£µ«ÊǽṹҪÇåÎúµÄ»°£¬Ò²²»ÊǺÜÄÑ¡£

¿´js´úÂ룺Ö𲽵Ĺ¹Ôì³öÒ»¸öjson¸ñʽ¡£

×îÖÕ£¬·¢ËͳöÈ¥µÄjson×Ö·û´®ÈçÏ£º

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • JqueryÖÐ$.postºÍ$.ajaxµÄÓ÷¨·ÖÎö

    JqueryÖÐ$.postºÍ$.ajaxµÄÓ÷¨·ÖÎö

    2016-08-13 14:00

  • ÈçºÎÔÚasp.netÏÂÀûÓÃAJAX¼¼ÊõʵÏÖÊ÷ÐÍĿ¼½á¹¹

    ÈçºÎÔÚasp.netÏÂÀûÓÃAJAX¼¼ÊõʵÏÖÊ÷ÐÍĿ¼½á¹¹

    2016-08-13 12:01

  • AjaxµÄXMLHttoRequest¶ÔÏó

    AjaxµÄXMLHttoRequest¶ÔÏó

    2016-08-13 11:02

  • PHP+jquery+ajaxʵÏÖ¼´Ê±ÁÄÌ칦ÄÜʵÀý_php¼¼ÇÉ

    PHP+jquery+ajaxʵÏÖ¼´Ê±ÁÄÌ칦ÄÜʵÀý_php¼¼ÇÉ

    2016-08-11 10:00

ÍøÓѵãÆÀ
»