Using content-type = text/uril-list and sending URL

I use POSTMAN client to test a webservice call. So I have two URLS,

  1. Main URL - http://localhost:8080/MyApp/api/projects/123/company
  2. Body URL - http://localhost:8080/MyApp/api/company/432

The request looks like the following in POSTMAN (screenshot below):

And I have set the the Content-Type as text/uri-list as shown below screenshot in the Headers section:

image

I am wondering what would be a best way to call using jQuery Ajax.

For example where would the second URL would go? The Body URL I mean.

$.ajax({
  url:`http://localhost:8080/MyApp/api/projects/123/company`,
  type:"POST",
  data:data,
  contentType:"text/uri-list",
  success: function(){
    ...
  }
})

It’s been a while since I used jQuery, but according to the jQuery.ajax() docs, it’s the data property.

Data to be sent to the server. If the HTTP method is one that cannot have an entity body, such as GET, the data is appended to the URL.

That to me implies that when the HTTP method can have an entity body, that the data property is that entity body.

1 Like

Thanks. So I will try second URL in place of data then. Probably something like this?

$.ajax({
  url:`http://localhost:8080/MyApp/api/projects/123/company`,
  type:"POST",
  data:`http://localhost:8080/MyApp/api/company/432`,
  contentType:"text/uri-list",
  success: function(){
    ...
  }
})

May I know what alternative to jQuery you are using currently? Thanks !

For Ajax related functionality, either Axios, or just the plain old Fetch API.

1 Like

Thanks. Can I just plug-in Axios instead of ajax and I am just good to go if I am using plain Javascript? The link yo mentioned is requiring npm I think.

There is a link to a CDN on the npm page. You can grab it from there.

Sorry, I don’t understand the question :slight_smile:

But this would be the equivalent of your code from post#3:

axios({
  method: 'post',
  headers: { 'Content-Type': 'text/uri-list' },
  url: 'http://localhost:8080/MyApp/api/projects/123/company',
  data: {
    url: 'http://localhost:8080/MyApp/api/company/432',
  }
}).then((result) => {
  // Do something with result
});

We have a nice guide on using Axios over on the main site.

Sorry about the confusion. I was wondering if I would have to download npm to use Axios in my code. But if a CDN link is available just like a jQuery then I could just include that and start using Axios in my code, right? Thanks for your inputs.

Yup. That’s right.

Thank you. Found it <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

I should probably switch to this then and stop using Ajax.

You’re getting your terminology slightly confused.

Ajax is a method of exchanging data with a server, and updating parts of a web page – without reloading the entire page.

jQuery is a JavaScript library. It has an $.ajax() function, which you can use for the above purpose.

So I guess the sentence should read “I should probably switch to this then and stop using jQuery.”

And the answer to that is fully, it dependsTM.

If you are using jQuery elsewhere on your site and you are happy with it, then there is no need to abandon it. I would not recommend adding Axios as an extra dependency for the sake of it.

However, if you are only using jQuery to make Ajax requests, then axios might be a good fit for you. It has a smaller footprint and (IMO) a nicer API.

1 Like

Thank you for elaborating.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.