Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devsam247's avatar

Submitting form to Ajax Post type

I have multiple Ajax calls (Get and Post) on a single page of Laravel Application, I want to post a form using AJAX Post but the form is using (the first Ajax on the page which has the) Get type. How can I make the form use the second Ajax with the Post type?

This is a snippet for the problem as the application is very large to show its code.

 $.ajax({  //First Ajax
          type: "GET",
          url: "/course/digital-marketing/facebook/fetch",       
          success: function (data) {

               function createOrUpdate(){
                document.getElementById("testForm").submit();// Form submission

                $.ajax({//Second Ajax
                    data: $('#testForm').serialize(),
                    url: "/course/digital-marketing/facebook",
                    type: "POST",
                    dataType: 'json',
                    success: function (data) {
                        console.log('Success:', data);
                        
                    },
                    error: function (data) {
                        console.log('Error:', data);
                    }

                });

            }

              createOrUpdate()

          }

0 likes
1 reply
Tray2's avatar

I use two methods for ajax calls, one post and one get

async function getRequest(url, parameters = {}, loading = true) {
  try {
      if (loading) {
          showLoading();
      }
      let responsePromise = await fetch(url, { method: 'GET', headers: parameters});
      if(! responsePromise.ok) {
          if(loading) {
              hideLoading();
          }
          throw new Error(responsePromise.status );
      }
      if (loading) {
          hideLoading();
      }
      return await responsePromise.json();
  } catch (error) {
      return error;
  }
}

async function postRequest(url, parameters = {}, loading = true) {
  try {
      if (loading) {
          showLoading();
      }
      let responsePromise = await fetch(url, { 
          method: 'POST',
          headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
          },
          body: new URLSearchParams(parameters)
      });
      if(! responsePromise.ok) {
          if (loading) {
              hideLoading();
          }
          throw new Error(responsePromise.status );
      }
      if (loading) {
          hideLoading();
      }
      return await responsePromise.json();
  } catch (error) {
      return error;
  }
}

Please or to participate in this conversation.