﻿
{"id":3436,"date":"2015-12-24T21:45:34","date_gmt":"2015-12-24T21:45:34","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=3436"},"modified":"2015-12-24T21:45:34","modified_gmt":"2015-12-24T21:45:34","slug":"jquery-ile-post-fonksiyonu-sayesinde-ajax-post-islemi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/jquery-ile-post-fonksiyonu-sayesinde-ajax-post-islemi\/","title":{"rendered":"jQuery \u0130le $.post Fonksiyonu Sayesinde Ajax Post \u0130\u015flemi"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Bu yaz\u0131m\u0131zda jQuery ile Ajax i\u015flemlerini daha pratik yapabilmemizi sa\u011flayan <strong>$.post<\/strong> fonksiyonu \u00fczerine konu\u015faca\u011f\u0131z.jQuery 1.0 versiyonuna eklenmi\u015f olan bu fonksiyon sayesinde daha h\u0131zl\u0131 ve kullan\u0131\u015fl\u0131 bir bi\u00e7imde Client tabanl\u0131 Server ili\u015fkilerimizi kurabilece\u011fiz.<\/p>\n<p>\u0130\u00e7eri\u011fimizi Asp.NET MVC tabanl\u0131 bir \u00f6rneklendirmeyle zenginle\u015ftirece\u011fimizi bilmenizi isterim.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        \/\/jQuery komutlar\u0131m\u0131z\u0131 bar\u0131nd\u0131racak olan Index Action'\u0131n .cshtml dosyas\u0131d\u0131r.\r\n        public ActionResult Index()\r\n        {\r\n            return View();\r\n        }\r\n\r\n        \/\/Post neticesinde tetikleyece\u011fimiz \u00f6rnek metodumuzdur.\r\n        &#x5B;HttpPost()]\r\n        public ActionResult Post(string a, bool? b)\r\n        {\r\n            return Json(&quot;Post i\u015flemi ba\u015far\u0131yla ger\u00e7ekle\u015ftirildi.&quot;, JsonRequestBehavior.AllowGet);\r\n        }\r\n<\/pre>\n<p>Evet, \u015fimdi $.post fonksiyonuna de\u011finmeden evvel $.ajax fonksiyonunu hat\u0131rlayal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;input type=&quot;submit&quot; value=&quot;Post Et&quot; id=&quot;btnPost&quot; \/&gt;\r\n\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\n    $(document).ready(function () {\r\n        $(&quot;#btnPost&quot;).click(function () {\r\n            $.ajax({\r\n                url: '@Url.Action(&quot;Post&quot;,&quot;Home&quot;)',\r\n                type: 'POST',\r\n                dataType: 'json',\r\n                data: { a: &quot;\u00f6rnek&quot;, b: true },\r\n                success: function (gelenveri) {\r\n\r\n                },\r\n                error: function (hata) {\r\n\r\n                }\r\n            });\r\n        });\r\n    });\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer bildi\u011fimiz $.ajax fonksiyonu arac\u0131l\u0131\u011f\u0131yla yap\u0131lan Ajax i\u015flemini \u00f6rneklendirmi\u015f olduk.Bu yap\u0131, g\u00fcn\u00fcm\u00fcz\u00fcn en \u00f6nemli fakt\u00f6r\u00fc olan zaman kavram\u0131n\u0131 bizim aleyhimize i\u015flemekte ve bunun yan\u0131nda dolayl\u0131 olarakta h\u0131z limitimizi s\u0131n\u0131rlamaktad\u0131r.($.ajax) fonksiyonunun g\u00f6vdesinde &#8220;url&#8221;, &#8220;dataType&#8221; vs. gibi genel yap\u0131n\u0131n kal\u0131plar\u0131n\u0131 olu\u015fturma mecburiyeti $.post fonksiyonunda giderilmektedir.<\/p>\n<p>\u015eimdi $.post fonksiyonunun prototipini ele alal\u0131m.<\/p>\n<blockquote><p>\n<em><strong>jQuery.post( url [, data ] [, success ] [, dataType ] )<\/strong><\/em><\/p><\/blockquote>\n<p>$.post fonksiyonunun en b\u00fcy\u00fck getirisi parametrik bir yap\u0131 olmas\u0131d\u0131r.Yani post an\u0131nda tetiklenecek adresin, verilecek parametre de\u011ferlerinin kal\u0131plar arac\u0131l\u0131\u011f\u0131yla de\u011fil, fonksiyonun parametresi arac\u0131l\u0131\u011f\u0131yla direkt olarak verilebilmesidir.<\/p>\n<p>\u015eimdi $.post fonksiyonunu kullan\u0131m varyasyonlar\u0131yla beraber ele alal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;input type=&quot;submit&quot; value=&quot;Post Et&quot; id=&quot;btnPost&quot; \/&gt;\r\n\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\n    $(document).ready(function () {\r\n        $(&quot;#btnPost&quot;).click(function () {\r\n            $.post(&quot;@Url.Action(&quot;POST&quot;,&quot;Home&quot;)&quot;, function (gelenveri) {\r\n                alert(data);\r\n            });\r\n        });\r\n    });\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Yukar\u0131daki varyasyon en sade bi\u00e7imiyle bir post i\u015flemidir.\u0130lgili adres tetiklenecektir ve geri d\u00f6n\u00fclen sonu\u00e7 gelenveri parametresinde tutulacakt\u0131r.Yani bu tipte, url k\u0131sm\u0131m\u0131z birinci parametre iken, success k\u0131sm\u0131 ikinci parametreye tekab\u00fcl etmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;input type=&quot;submit&quot; value=&quot;Post Et&quot; id=&quot;btnPost&quot; \/&gt;\r\n\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\n    $(document).ready(function () {\r\n        $(&quot;#btnPost&quot;).click(function () {\r\n            $.post(&quot;@Url.Action(&quot;POST&quot;,&quot;Home&quot;)&quot;, { a: &quot;\u00f6rnek&quot;, b: true }, function (gelenveri) {\r\n                alert(gelenveri);\r\n            });\r\n        });\r\n    });\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Post neticesinde g\u00f6nderilecek parametre de\u011ferlerinide 2. parametreden bu \u015fekilde verebiliyoruz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;input type=&quot;submit&quot; value=&quot;Post Et&quot; id=&quot;btnPost&quot; \/&gt;\r\n\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\n    $(document).ready(function () {\r\n        $(&quot;#btnPost&quot;).click(function () {\r\n            $.post(&quot;@Url.Action(&quot;Post&quot;,&quot;Home&quot;)&quot;, function () {\r\n                \/\/Success\r\n                alert(&quot;Gelen sonucu elde eder.&quot;);\r\n            }).done(function () {\r\n                alert(&quot;\u0130kinci success olarak d\u00fc\u015f\u00fcnebiliriz.&quot;);\r\n            }).fail(function () {\r\n                alert(&quot;Ajax i\u015fleminde bir hata al\u0131nd\u0131ysa devreye girer.&quot;);\r\n            }).always(function () {\r\n                alert(&quot;T\u00fcm ajax i\u015flemi bitti\u011finde devreye girer.&quot;);\r\n            });\r\n        });\r\n    });\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Yukar\u0131da g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi &#8220;done&#8221;, &#8220;fail&#8221; ve &#8220;always&#8221; fonksiyon eklemleriyle Ajax s\u00fcrecinde kontrol sa\u011flayabiliyoruz.<\/p>\n<p>\u015eimdi a\u015fa\u011f\u0131da seri bir \u015fekilde birka\u00e7 \u00f6rneklendirme yapal\u0131m.<\/p>\n<p>A\u015fa\u011f\u0131daki kod sadece post i\u015flemi yapmaktad\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            $.post(&quot;@Url.Action(&quot;Post&quot;,&quot;Home&quot;)&quot;);\r\n<\/pre>\n<p>A\u015fa\u011f\u0131daki kod post i\u015flemi yan\u0131nda ilgili parametre de\u011ferlerini vermektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            $.post(&quot;@Url.Action(&quot;Post&quot;, &quot;Home&quot;)&quot;, { a: &quot;\u00d6rnek&quot;, b: true });\r\n<\/pre>\n<p>A\u015fa\u011f\u0131daki kod post i\u015flemi yan\u0131nda success fonksiyonunu \u00e7al\u0131\u015ft\u0131rmaktad\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            $.post(&quot;@Url.Action(&quot;Post&quot;, &quot;Home&quot;)&quot;, function (gelendeger) {\r\n                alert(&quot;Gelen De\u011fer : &quot; + gelendeger);\r\n            });\r\n<\/pre>\n<p>A\u015fa\u011f\u0131daki kod post i\u015flemi yan\u0131nda parametre de\u011ferlerini vermekte ve done fonksiyonu sayesinde de success fonksiyonu devreye sokmaktad\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            $.post(&quot;@Url.Action(&quot;Post&quot;, &quot;Home&quot;)&quot;, { a: &quot;\u00d6rnek&quot;, b: true }).done(function (gelenDeger) {\r\n                alert(&quot;Gelen De\u011fer : &quot; + gelenDeger);\r\n            });\r\n<\/pre>\n<p>A\u015fa\u011f\u0131daki kod JSON bir veri olu\u015fturup bu veriyi post etmekte ve done fonksiyonu ile success fonksiyonunu devreye sokmaktad\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;script type=&quot;text\/javascript&quot;&gt;\r\n    $(document).ready(function () {\r\n\r\n        var Entity = {\r\n            A: &quot;Gen\u00e7ay&quot;,\r\n            B: true\r\n        };\r\n\r\n        $(&quot;#btnPost&quot;).click(function () {\r\n            $.post(&quot;@Url.Action(&quot;Post&quot;, &quot;Home&quot;)&quot;, Entity).done(function (gelenDeger) {\r\n                alert(gelenDeger.A + &quot; &quot; + gelenDeger.B);\r\n            });\r\n        });\r\n    });\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>Bu yaz\u0131m\u0131z\u0131nda sonuna gelmi\u015f bulunmaktay\u0131z.<\/p>\n<p>Hepinize iyi \u00e7al\u0131\u015fmalar dilerim.Sonraki yaz\u0131mlar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Bu yaz\u0131m\u0131zda jQuery ile Ajax i\u015flemlerini daha pratik yapabilmemizi sa\u011flayan $.post fonksiyonu \u00fczerine konu\u015faca\u011f\u0131z.jQuery 1.0 versiyonuna eklenmi\u015f olan bu fonksiyon sayesinde daha h\u0131zl\u0131 ve kullan\u0131\u015fl\u0131 bir bi\u00e7imde Client tabanl\u0131 Server ili\u015fkilerimizi kurabilece\u011fiz. \u0130\u00e7eri\u011fimizi Asp.NET&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3401,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,19,780],"tags":[744,854,576,853,855,856,440],"class_list":["post-3436","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","category-jquery","category-json","tag-ajax","tag-always","tag-done","tag-fail","tag-jquery-post","tag-jquery-ajax","tag-post"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/3436","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=3436"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/3436\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3401"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=3436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=3436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=3436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}