`
In jquery, cross-domain data can be used in the success function. The principle is to use the src of the script note to cross-domain. Add ?callback=func to the url; func is added by jq A temporary global function...
`
I found this picture on Baidu in angular for a long time. The tutorial said that after adding callback=JSON_CALLBACK to the url (it must be written as JSON_CALLBACK), when the server responds, the success function will be executed. However, I did it and reported an error. Got it!
Then I changed the code to this, no error was reported, and the cross-domain was successful, but ajax is asynchronous. How to use the obtained data in the angular controller?
閉關(guān)修行中......
jsonp
,服務(wù)器端是根據(jù)你后面callback的函數(shù)名,給你返回一段包裝的json
數(shù)據(jù),并不一定就是JSON_CALLBACK
, you can choose the name at will, but your js file must have a consistent function name. You can read this article
jsonp
Cross-domain needs to be set up in the background, so it has nothing to do with angular cross-domain. . .
There are many ways, the more elegant one is to create onefactory
,或者service
,whatever,返回一個(gè)對(duì)象,factory
或service
里異步請(qǐng)求數(shù)據(jù),返回的數(shù)據(jù)存儲(chǔ)到返回的對(duì)象下,屬性名隨你起,然后吧這個(gè)玩意注入到你想使用的controller
里,但是因?yàn)槭钱惒降?,所?code>controller執(zhí)行的時(shí)候可能返回的對(duì)象還是空,需要bind到$scope
上才能自動(dòng)更新。要不你可以隨意一點(diǎn),直接在controller
里異步執(zhí)行,回調(diào)里直接給到$scope
.
If asynchronous data needs to be displayed immediately, it is better to execute asynchronously first and initialize angular in the callback.