Lukas Taylor
Lukas Taylor

Reputation: 11

Strip initial question mark from ajax request in jQuery

So I am trying to send an Ajax request to a server but I need to strip the initial question mark from the request

the code:

$(document).ready(function(){
  (function($){
      function processForm( e ){
        var callid = $('.callid').val();
        var pin = $('.pin').val();
        var urlFinal = callid+'/'+pin;
          $.ajax({
              url: 'https://httpbin.org/get/',
              dataType: 'text',
              type: 'get',
              contentType: 'application/x-www-form-urlencoded',
              //$(this).serialize()
              data: urlFinal,
              success: function( data, textStatus, jQxhr ){
                console.log('success' + data.streamFileUrl)
                $('.overlay').show();
                $('#video').html( data.streamFileUrl );
              },
              error: function( jqXhr, textStatus, errorThrown ){
                  console.log('OH NOES');
                  $('.incorrect').show()
              }
          });

          e.preventDefault();
      }

      $('#form').submit( processForm );

  })(jQuery);

})

The Result:

https://httpbin.org/get/?1234/1234

What I'm after:

https://httpbin.org/get/1234/1234

Upvotes: 1

Views: 898

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337626

As you're sending a GET request jQuery will automatically add data to the querystring for you.

To avoid this, append urlFinal to the string you provide to url instead of adding it to the data argument.

var urlFinal = callid + '/' + pin;
$.ajax({
  url: 'https://httpbin.org/get/' + urlFinal,
  dataType: 'text',
  type: 'get',
  contentType: 'application/x-www-form-urlencoded',
  success: function(data, textStatus, jQxhr) {
   // ...
  },
  // ...
});

Upvotes: 2

Krypt1
Krypt1

Reputation: 1066

It seems that you can just append the urlFinal to baseUrl like so:

$(document).ready(function(){
  (function($){
      function processForm( e ){
        var callid = $('.callid').val();
        var pin = $('.pin').val();
        var urlFinal = callid+'/'+pin;
          $.ajax({
              url: 'https://httpbin.org/get/' + urlFinal,
              dataType: 'text',
              type: 'get',
              contentType: 'application/x-www-form-urlencoded',
              success: function( data, textStatus, jQxhr ){
                console.log('success' + data.streamFileUrl)
                $('.overlay').show();
                $('#video').html( data.streamFileUrl );
              },
              error: function( jqXhr, textStatus, errorThrown ){
                  console.log('OH NOES');
                  $('.incorrect').show()
              }
          });

          e.preventDefault();
      }

      $('#form').submit( processForm );

  })(jQuery);

})

Upvotes: 0

Related Questions