-4

I need to write down class which will do ajax request in jquery variables: -contentType-form/other, default form -method - get from attr(method)

if form or from data(method) if other -data -url- get from attr(action) if form or from data(action) if other methods -loadDataFromForm

Now I have somethink like that:
http://jsfiddle.net/h7kRt/507/

As You can see when you click Show Results button its works but when you chceck network it send data not from FORM but from button why?? Somone can help?

1 Answers1

0

Something like this?

$(document).ready(function() {
   $('[data-request-url]').on('click',function(evt) {
       var $target = $(evt.currentTarget);
       var url = $target.data('request-url');
       $.ajax(url).success(function(data) {
           console.log('done',data);
       }).error(function(data){console.error(data);});
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a data-request-url="https://jsonplaceholder.typicode.com/posts/1">abc</a>
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • 2
    Giving the OP code snippets won't solve their issue of a lack of understanding. (Why _did_ you answer such a low effort question?) – evolutionxbox Jun 21 '17 at 10:01
  • mostly cause I felt like it honestly. maybe it'll give him a lead where to research. – Tschallacka Jun 21 '17 at 10:03
  • Maybe, but I doubt the OP will understand what you've written. They'll likely use it and then complain when it does exactly fit their needs. – evolutionxbox Jun 21 '17 at 10:05
  • true, but in a lower quality question, i'm not really in the mood to elaborate. I felt a bit like coding, not like wasting time on a low quality question that'll be closed soon enough. – Tschallacka Jun 21 '17 at 10:06
  • Fair enough. ¯|_(ツ)_/¯ – evolutionxbox Jun 21 '17 at 10:07
  • Thanks for answer, i know how to make ajax calls, my problem is to make a class that i can call whenever i want and with options i want. like contentType or method and url. – Bartek Mikołajczuk Jun 21 '17 at 10:30
  • so basically you wish to be able to instantiate an class that has an url and certain parameters set, that you then can use to request an url over and over, with different data to send along? something like `var user = new UserQuery('/userstat.php');user.get({user_id:1});`? – Tschallacka Jun 21 '17 at 10:52
  • Yes yes but i i dont know where to start how to process options to class and i dont know how to handle contentType it should be by default form but can be also other ;) but when its form i need to take method from attr(method) and url form(action) but when its other i need it form data(method). I dont full understand what means contentType and when its form. – Bartek Mikołajczuk Jun 21 '17 at 10:56
  • So I assume this is homework. Can you simply post the entire assignment as question, with especially focussed on the part you don't understand. Since you understand how to do ajax requests. – Tschallacka Jun 21 '17 at 11:32