²©Ö÷µÄ¸ü¶àÎÄÕÂ>>
ÔÙ̸Jquery Ajax·½·¨´«µÝµ½action
2012-09-07 14:15:03±êÇ©£ºMVC
Ô´´×÷Æ·£¬ÔÊÐíתÔØ£¬×ªÔØʱÇëÎñ±ØÒÔ³¬Á´½ÓÐÎʽ±êÃ÷ÎÄÕ Ôʼ³ö´¦ ¡¢×÷ÕßÐÅÏ¢ºÍ±¾ÉùÃ÷¡£·ñÔò½«×·¾¿·¨ÂÉÔðÈΡ£
֮ǰд¹ýһƪÎÄÕÂJquery Ajax·½·¨´«Öµµ½action£¬±¾ÎÄÊǶԸÃÎĵIJ¹³ä¡£
¼ÙÉè controllerÖеķ½·¨ÊÇÈçÏ£º
ÆäÖÐPersonModel¶¨ÒåÈçÏ£º
ÄÇôcontroller·½·¨·Ö±ð½ÓÊܵ¥¸ömodelºÍÒ»¸ömodelµÄList¡£²ÉÓÃͨ¹ýajax´«µÝ²ÎÊý¡£
¶ÔÓÚ´«µÝµ¥¸ö²ÎÊýµÄÇé¿ö,¼ÙÉèjs´úÂëÈçÏ£º
´ÓchromeÖнØͼ¿ÉÒÔ¿´µ½ÈçÏ£º
´«µÝµÄÊý¾ÝÊÇÒ»´®FormÊý¾Ý£¬¸ù¾ÝÃüÃûÆ¥ÅäµÄÔÔò£¬Ò²ÊÇ¿ÉÒÔÈ¡µÃÊý¾ÝµÄ¡£
½«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"
Òý×Ô
¿´ÇëÇóÍ·µÄ½Øͼ£º
Òò´Ë£¬´«µÝµ½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¡£
Èç¹û°ÑprocessDataÉèÖÃΪfalse¡£
ÒÔÉÏÁ½ÖÖ·½Ê½£¬°´ÕÕapplication/jsonµÄÀàÐÍ´«¸ø¶¼»áʧ°Ü£¬ÒòΪjsonÊÇ»ùÓÚÎı¾µÄ¸ñʽ£¬ÉÏÃæÁ½ÖÖ·½Ê½´«µÝµÄ¶¼²»ÊÇjsonÎı¾¡£Òò´Ë»á³ö´í¡£
Òò´Ë£¬°Ñoption¸Ä³É£º
Ôò´«µÝµÄ¾ÍÊÇjsonÎı¾£¬Òò´Ë¸ù¾ÝÃüÃûÆ¥Å䣬¾ÍÄÜ»ñµÃÖµÁË¡£
¶ÔÓÚ½ÏΪ¼òµ¥ÊÇÊý¾ÝÀàÐÍ£¬ÓÐʱºò²»Ö¸¶¨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µÄ·½Ê½´«µÝµÄ£¬
¿ÉÒÔ´Ó½ØͼÖп´µ½¡£µ«ÊÇÕâÖÖ¸ñʽµÄÊý¾Ý£¬controllerÖÐÖ»ÄÜ»ñµÃÖ¸¶¨modelÓÃ2¸öÔªËØ£¬ÎÞ·¨»ñµÃÔªËØÖÐÊôÐÔµÄÖµ¡£
Èç¹û°Ñdata¸Ä³ÉJSON.stringify(jsonp)£¬ÈçÏ£º
ÄÇô´«µÝ¹ýÈ¥µÄForm DataÊÇÒ»´®×Ö·û´®£¬controller¸úÎÞ·¨Ê¶±ð³öÕâ¸ö¶«Î÷£¬Òò´Ë»ñ²»µ½Öµ¡£Èç¹û½ö½öÉèÖÃcontentType: 'application/json',¶ø´«µÝµÄÓÖ²»ÊÇjson¸ñʽµÄÊý¾Ý£¬ÈçÏ£º
ÒòΪjqueryµÄajax·½·¨»á°Ñdataת»»³É²éѯ×Ö·û´®£¬Òò´Ë¾Í±ä³ÉÈçϵÄÑù×Ó¡£Õâ´®Îı¾µ±È»²»·ûºÏjson¸ñʽ£¬Òò´Ë»á³öÏÖÏÂÃæµÄ´íÎó¡£
Èç¹ûÉèÖÃcontentType: 'application/json',²¢ÇÒÉèÖÃdata: JSON.stringify(persons)£¬ÈçÏ£º
ÄÇô¿ÉÒÔ»ñµÃµ½ÕæÕýÍêÕûµÄjsonÊý¾ÝÁË
×îºó£¬´Ë´¦ÔÙÑÝʾһ¸ö¸ü¸´ÔӵIJÎÊýÀàÐÍ£¬ÒÔ±ã¼ÓÉîÀí½â¡£
Ê×ÏÈ¿´Ò»ÏÂControllerÖеķ½·¨Ç©Ãû£¬TestClassB ºÍÒ»¸öTestClassAµÄList¡£ÉÔÏÔ¸´ÔÓ¡£
ÔÙ¿´TestClassAºÍTestClassB£¬¸üÏÔ¸´ÔÓ¡£µ«ÊǽṹҪÇåÎúµÄ»°£¬Ò²²»ÊǺÜÄÑ¡£
¿´js´úÂ룺Ö𲽵Ĺ¹Ôì³öÒ»¸öjson¸ñʽ¡£
×îÖÕ£¬·¢ËͳöÈ¥µÄjson×Ö·û´®ÈçÏ£º
¡¡