开发者

Get data by ajax or include a javascript file

开发者 https://www.devze.com 2023-03-23 06:04 出处:网络
I have a widget which has to fetch json data. The json data will change... I have two options to include the content in the boot-loader file.

I have a widget which has to fetch json data. The json data will change...

I have two options to include the content in the boot-loader file.

-----------------------------------------------------------------------------------------------------------

  • Include a javascript file with the data and a function to process that data as the file is included completely.

OR

  • Use ajax to get the json data.

-----------------------------------------------------------------------------------------------------------

  1. Which has better interface?

    开发者_如何学C
  2. Which is easier to code?

  3. Which is less error prone?

  4. Which is more compatible to browsers?

  5. Which is lighter and faster to execute on the client side?


you are asking the wrong questions here.

if your widget will use static data, i mean the json data will not change before you refrech the page , then you can include the data in the js file you are serving.

in the other hand, if you plain to refrech your widget with new data without refreching the page (on some action for example clicking a refrech button) you will need to get the data with an ajax call to the server.

you may also combine the two methods. on the first load of the page you get the data on the js file then on each refrech make an ajax call to some function in the server wich returns the refreched data you need.

in the two cases you will need to parse the data you are getting from json to javascript object. hopefully that's not very difficult & you can get many examples on how to use json data by just searching on google.

maybe you can be more specific about the widget type or what it does so i can help you more .


Which has better interface? javascript inclusion is easier to use. also you wouldn't need to use jquery so that would reduce the size of the widget as well.

Which is easier to code? javascript inclusion is easier to use.

Which is less error prone? Which is more compatible to browsers? javascript inclusion method of jsonp which you are using is cross-domain compatible and there are going to be less errors with it.

Which is lighter and faster to execute on the client side? depends really...but there shouldn't much difference in execution but rather fetching the widget which shouldn't be much different either.

Use javascript file inclusion

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号