Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement cross-domain requests with jQuery+JSONP

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how to use jQuery+JSONP to achieve cross-domain request related knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

   problem: there is a local page demo.html that needs to get data from http://localhost:3561/User/GetAllNames and display it.

   answer: since the two parties in the question are not on the same server, you need to use jsonp for cross-domain access.

  ① client writing

The    client uses the $.getJson method provided in jQuery for cross-domain access. GetJson has three parameters:

     I. url: request address

     II. Data: parameters sent to the server

     III. Callback: callback function if successful

The use of    getJson is basically the same as the ordinary $. Get method, except that getJson needs to add callback=? to the parameters after url This fixed part will be automatically replaced by jQuery. Is the correct function name to execute the callback function. Then the json object returned from a foreign domain is manipulated in the callback function, and the parameter of the callback function callback is the json object.

$.getJSON ("http://localhost:3561/User/GetAllNames?callback=?", function (json) {for (var I = 0; I)

< json.length; i++) { $("#nameList").append("" + json[i] + ""); } });   ② 服务端编写   服务端的逻辑主要是将数据序列化为json字符串,然后封装成"callback(json)"的形式,callback为jQuery自动生成并传到服务端的函数名称。下面使用C#实现: public class UserController : Controller{ public string GetAllNames(string callback) { string[] names = new string[] { "张三丰", "张无忌", "令狐冲", "杨过", "郭靖" }; JavaScriptSerializer jss = new JavaScriptSerializer(); string json = jss.Serialize(names); return string.Format("{0}({1})", callback, json); }}   至此,便成功解决了问题。

  thinks: if the server has already written callback (e.g. return string.Format ("moty ({0})", json);), how should the client write it?

  reference:

Ajax ("http://localhost:3561/User/GetAllNames", {jsonpCallback:" moty ", dataType:" jsonp ", success: function (json) {for (var I = 0; I < json.length; iTunes +) {$(" # nameList "). Append ("+ json [I] +");}); these are all the contents of the article "how to use jQuery+JSONP to implement cross-domain requests". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report