Today to do a go before the project to fetch data, intended to build an interface, WebServer. Encountered some problems in the middle. Is the problem of cross-domain value
Client page
1. First AJAX request Dataty: ' JSONP ' This format, but also add a JSONP: "Callback", the most important thing is that there is a callback function callback (), you can toss me bad. Directly on the client code:
<script type="Text/javascript">$ (function () {$.ajax ({type:"GET", URL:"Http://localhost:48748/WebService1.asmx/GetGrid", Data:"", DataType:"Jsonp", Jsonp:"Callback", Success:function (data) {callback (data); }, Error:function (Error) {Console.log (error); })}) function callback (data) {alert (data.rows[0]. Name); } </script>
Background code.
2. Using the webserver server, the specific configuration is not much to say, today's topic is the cross-domain access to JSON data, the need to pay attention to the problem is mentioned earlier, need to use a callback callback function, to have a certain format. Please look at the code:
[WebMethod] Public voidGetgrid () {List<Notice> list =NewList<notice>(); List. ADD (NewNotice {id=1, name="xiaoming"}); JavaScriptSerializer JSON=NewJavaScriptSerializer (); varGrid =New{rows =list}; Context.Response.Write ("Callback ("+json. Serialize (GRID) +")"); }
This thing has been going on for a day, written on: January 27, 2015 01:06:09
Considerations for the definition of Ajax cross-domain value JSONP