Sync Weibo or QQ space to Blog Park Wcf+js (Ajax) cross-domain requests (1)

Source: Internet
Author: User

The day before yesterday just wrote an article using bootstrap for your blog garden Custom Carousel picture (today will be loaded in the order of the picture, not directly loaded in the time of the visit, but the page load finally in the script dynamically add DOM elements), although the technical content is not high, but we are still interested. In fact, the most important thing for bloggers is that the points in the blog are rising. So take the strike. A post that uses WCF+JS Ajax to request data synchronization space across domains.

Because is the request QQ to say the data, so to log on my QQ, this is very troublesome, always can not let every visitor landing QQ, and then take out the data, and QQ also does not have the relevant interface to provide, landing time also to deal with the verification code. So the idea was immediately negated. Finally, I suddenly thought of WCF, so that everyone directly to my machine to fetch data is much safer. So finally, I decided to deploy a WCF service on my native computer and use JS in the blog Park to request data across domains. As for the QQ data will say. I intend to use the Chrome plugin when I log on to the QQ data storage, and then people come to me this time to access the data can be directly to take the saved data, data can be saved to the database, can also be saved in TXT file. Chrome plugin to take the data is good, but because JS can not directly manipulate the folder, so this problem has not been thought good, but HTML5 now also should have corresponding solution, this question for the time being not mention.

Configure WCF Services

The project structure is as follows

AjaxService.cs class

Using system;using system.collections.generic;using system.linq;using system.servicemodel;using System.servicemodel.activation;using system.servicemodel.web;using system.text;using System.Threading.Tasks; namespace iisservices{    [ServiceContract (Namespace = "")]    [Aspnetcompatibilityrequirements ( Requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]    [Javascriptcallbackbehavior ( Urlparametername = "Jsoncallback")] public    class Ajaxservice    {        [OperationContract]        [WebGet ( Requestformat = Webmessageformat.json)] public        string Test (string value)        {            return "Wcfservice:" + value;< c10/>}}}    

Ajaxservice.svc

<% @ServiceHost service= "Iisservices.ajaxservice"%>

Web. config

<?xml version= "1.0" encoding= "Utf-8"?><configuration> <system.web> <compilation debug= "true" ta rgetframework= "4.0"/> <!--added: Authentication, and set to Forms--> <authentication mode= "Forms"/> </system .web> <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name= "wcf.de Mo. Ajaxserviceaspnetajaxbehavior "> <enablewebscript/> </behavior> </endpointbehaviors > <!--add servicebehaviors--> <serviceBehaviors> <behavior name= "Enablemetadatabehaviors" > <servicemetadata httpgetenabled= "true"/> <servicedebug includeexceptiondetailinfaults= "Tru E "/> </behavior> </serviceBehaviors> <!--add servicebehaviors: End---</behaviors&    Gt <!--add bindings--> <bindings> <webHttpBinding> <binding name= "test" Crossdomainscriptac Cessenabled= "trUE ></binding> </webHttpBinding> </bindings> <!--add bindings: End-to-<servicehost      Ingenvironment aspnetcompatibilityenabled= "true" multiplesitebindingsenabled= "true"/> <services> <!--add to service: behaviorconfiguration: Metadata--<service name= "Iisservices.ajaxservice" behaviorconfiguration= "Enablemetadatabehaviors" > <!--added to endpoint: bindingconfiguration cross-domain---&LT;ENDP Oint address= "" behaviorconfiguration= "Wcf.demo.AjaxServiceAspNetAjaxBehavior" bindingconfiguration= "test" bind ing= "WebHttpBinding" contract= "Iisservices.ajaxservice"/> </service> <!--<service Behaviorconfig uration= "Enablemetadatabehaviors" name= "Iisservices.calculatorservice" > <endpoint binding= "wsHttpBinding" Co  ntract= "Iisservices.icalculator"/> </service>--> </services> </system.serviceModel> </configuration>

Build the service, we publish the project

  

Inside the bin is the DLL we generated.

Configure IIS

Now let's configure our IIS

Configure IIS, we need to do a port mapping, note the above, as long as the port mapping, public access will be transferred to our local port up.

To do port mapping, we also need to turn off the firewall.

Now we use the public network and intranet to visit our website

We found that two addresses were accessible to our service, so the configuration was successful.

Call the WCF service in the blog Park JS

We just need to add the following code to the footer script. Note that when debugging code, do not add alert in the blog Park script, I just started to join the alert, resulting in the entire JS is not loaded, this should be the authentication mechanism of the blog park. It was thought that the blog Park would not allow cross-domain access. A lot of time to find out to remove the alert is good.

  (function MyFunction () {        var url = "http://39.183.39.251:99/ajaxservice.svc/test?jsoncallback=?&";          $.getjson (URL, {"Value": "Cross-Domain"}, function (msg) {             $ (". HeaderText"). Append (msg);          });    }) ();

Now save our script and then visit our page to find out that there is a wcfservice in the top of my blog name: cross-domain fields, where the wcfservice is from my native query past.

 

At this point to talk about synchronization to the blog Park This project is half finished, the next need to take QQ space data, and then access the data in WCF.

For the moment, the following continues.

Another: Because the net inside the home, IP is fixed, so can find the data. Go to work tomorrow, notebook also to carry, so tomorrow estimated service can not be used, after all, can not do port mapping in the company.

So tomorrow should not see the effect.

Another: Because directly announced the IP of my machine, I hope the gods do not do hacker attacks. The firewall is off too.

This address: http://www.cnblogs.com/santian/p/4413361.html

Blog Address: http://www.cnblogs.com/santian/

Reprint please indicate the original source of the article in the form of hyperlink.

Sync Weibo or QQ space to Blog Park Wcf+js (Ajax) cross-domain requests (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.