An overview of the issues encountered in Ajax cross-domain access to WCF services.

Source: Internet
Author: User
Tags try catch

Tool Description: Vs2012,sql server 2008R2

1. First, build a WCF service project through vs2012 and build it. Open a new vs2012. Build a Web project that accesses services through jquery's AJAX approach.

Issue: Because Web projects and WCF services are not in the same port, there is a "cross-domain" issue. Cross-domain access requires a certain qualification of the interface and methods of the service. Specific reference: http://www.cnblogs.com/yangbingqi/p/2096197.html

2. Resolving cross-domain issues, we need to deploy the service to IIS. The deployment of vs2012 is relatively simple and does not dwell on it.

3. After deploying the WCF service to IIS, the Web-side accesses its IP address through Ajax, implementing the invocation of the service method. But the call has failed. Here's how:

public class Service1:iservice1 {public string GetData (int value) {//return string.            Format ("you entered: {0}", value); Return "{\" a\ ":" + "\" "+value.        ToString () + "\"} ";             Public Compositetype getdatausingdatacontract (Compositetype composite) {if (composite = = null)            {throw new ArgumentNullException ("composite"); } if (composite. Boolvalue) {composite.            StringValue + = "Suffix";        } return composite; } BLL. UserClass UC = new BLL.                UserClass (); public string Show_averagesalary_job (string city, string area) {//return "{\" a\ ":" + "\" + City. ToString () +area.                        ToString () + "\"} ";            DataTable dt=new DataTable ();            try {dt = uc.show_averagesalary_job (city, area); } catch (Exception e) {RETurn e.message;        } return Datatableconvertjson.datatable3json (DT); }    }

S1. Initially called Show_averagesalary_job this method, has been called failed. I suspect that the possible problems are as follows:

1). WCF configuration error, WCF configuration is complex, I am not familiar with this, I do not know its depth, so just began to be consistent tangled in the WCF configuration in IIS there is a problem.

2) The request data and the return data format question. See others on the web, the JSON and JSONP formats are different, remote calls can only be used Jsonp. I also struggle with this. The final proof: JSON and JSONP are OK under get requests.

3) online Help. "Let me look at the output of the console," said one enthusiast, "and by observing I find every request, the request does not return any status code," says F12. The feeling is very confusing.

4) View the IIS logs. I didn't see any clue at first. But finally, the log records a lot of information, and logs are very important information that can monitor the status of the IIS service.

S2. After the S1 step, I still did not find any problems, it is very confusing. I tried the remote Access GetData () This method and found that it was successful!

1) since GetData () has just started to return the data as simple character type (not JSON), and Show_averagesalary_job () This method returns the JSON format, I guess it is possible that the data format of the transmission problem.

2) after GetData () also return JSON type of data, again Remote call discovery is OK. Proving that the data format is not the place where the problem occurs. So the problem is obvious, and the problem is on the Uc.show_averagesalary_job method call.

3) Considering that this place involves database access, it is possible that IIS has to remotely access the database and then change the connection string from localhost to IP and configure the database to allow remote connections. The test is still not working.

4) Finally, I suspect that the string was the result of Windows authentication and was modified to be SQL Server Authentication (USER+PWD) login. Success! Finally caught out of this deep bug, tossing the whole day.

Harvest:

One. General Harvest

1. Familiarize yourself with some of the details of the WCF service during configuration.

2. Familiarize yourself with the considerations of Ajax calling WCF across domains.

Ii.. Commissioning aspects:

1. To learn how to analyze the IIS server health and analyze the program bugs through IIS logs;

2. Learn to control variables, step-by-try, and eventually lock up the problem-critical code. (as in this example, when looking for a problem, through GetData () to exclude the possibility of remote call communication errors, by trying to return the JSON GetData (), the possibility of the transmission of data format errors, and finally locked the core code of the problem. )

3. Make full use of the anomaly capture mechanism, such as at the time of dt = Uc.show_averagesalary_job (city, area); This access to the database is the first place to add such a try Catch statement, You may be able to locate it faster because of a problem with database access. So, the use of exception trapping is really skill alive.

Finally attach the example "I want to download"

An overview of the issues encountered in Ajax cross-domain access to WCF services.

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.