Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

web service work when call from local not work call from other hosting

i create web service to add data (sent from anther server) to my data

Code web service
C#
public string SaveData(string resultData) //save data to database
       {

       sa_asdm.DLR service = new sa_asdm.DLR();
           RootObject obj = JsonConvert.DeserializeObject<RootObject>(resultData);

         /*string filePath = Server.MapPath("~/Error.txt");

           using (StreamWriter writer = new StreamWriter(filePath, true))
           {
               writer.WriteLine("Json is sent :" + resultData +
                  "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
               writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
           } */

              foreach (JsonData item in obj.results)
                  {
                      {
                          var connStr = ConfigurationManager.ConnectionStrings["myCon1"].ConnectionString;
                          SqlConnection con = new SqlConnection(connStr);
                          try
                          {
                              //------------------------------------

                              //------------------------------------
                              SqlCommand cmd = new SqlCommand("[insert_RDL]", con);
                              cmd.CommandType = CommandType.StoredProcedure;
                              cmd.Parameters.AddWithValue("@msgId", item.msgId);
                              cmd.Parameters.AddWithValue("@to_mobile", item.to);
                              cmd.Parameters.AddWithValue("@status", item.status);
                              con.Open();
                              cmd.ExecuteNonQuery();
                          }
                          finally
                          {
                              //
                          }
                      }
                  }
           return "OK ";
       }


sent page for example :

JavaScript
<script type="text/javascript">
   //  var tt = "[{ 'status': 500, 'ResponseStatus': 'NotConnect' }]";

     var json = '{"results": [{"msgId": "001","to": "9665312114","status": "D"}, {"msgId": "859911880","to": "966535112578","status": "N"}, {"msgId": "859911880","to": "966535112579","status": "S"}]}';
         $.ajax({
             type: "POST",
             url: "http://website1.com/DLR.asmx/SaveData",
             contentType: "application/json; charset=utf-8",
             data: JSON.stringify({ resultData: json }),// pass json string to web service
             traditional: true,
             processData: false ,
             success: function (data) { }

         });
 </script>


the web service in website1

when i post json from same server : http://website1.com/sent.aspx working

But when i post json from other server for example : اttp://website2.com/sent.aspx Not working

-------------------------------------------------------

What I have tried:

i sloved it i add this code to web.config
C#
<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS, PUT, DELETE" />
      <add name="Access-Control-Allow-Headers" value="content-type,soapaction,x-requested-with" />
    </customHeaders>
  </httpProtocol>
</system.webServer>
Posted
Updated 31-Jan-18 0:21am
v2
Comments
[no name] 29-Jan-18 7:58am    
Check the URL in Ajax component.That should be http://website2.com/DLR.asmx/SaveData
F-ES Sitecore 29-Jan-18 8:03am    
Use a tool like Fiddler, or your browser's dev tools (f12->network) to look at the response, it might be a CORS issue, if so google for more info + solutions.
ZurdoDev 29-Jan-18 9:49am    
You have to debug this. We can't see what is happening.
Member 12857222 29-Jan-18 11:34am    
can you test :
http://asdm.sa/Sent.aspx this work Because in the same server web service

http://exams.medu.sa/sent.aspx this not work

Your issue is that you are trying to make a request from domain A to a web service on domain B via client side javascript. The issue you are running into is cross origin resource sharing is blocking your request to the web service on domain A.

Basically, if you are able to, you need to enable CORS requests for your web service.

I think you have 2 options

* I don't know if this is web api but you need to figure out how to enable cors requests to your web service.
Google[^]

enable cors in asp.net mvc - Google Search[^]

Google[^]

Those 3 google searches have a ton of relevant links, you'll need to do the leg work on your own to figure out what is going to work for your project.

* The other option is that you add a server side method/action to your project where you then adjust your $.ajax call in your client side to call, and from within that newly added server side code you make the call out to Domain A's web service which bypasses the CORS issue.
 
Share this answer
 
Comments
Laxmidhar tatwa technologies 30-Jan-18 9:16am    
Yes it is current .but may some security issue
when i post http://asdm.sa/Sent.aspx the return is : OK
this code from F12
Request URL:http://asdm.sa/DLR.asmx/SaveData<br />
Request Method:POST<br />
Status Code:200 OK<br />
Remote Address:67.209.123.6:80<br />
Referrer Policy:no-referrer-when-downgrade<br />
Response Headers<br />
view source<br />
Cache-Control:private, max-age=0<br />
Content-Length:11<br />
Content-Type:application/json; charset=utf-8<br />
Date:Mon, 29 Jan 2018 16:31:58 GMT<br />
Server:Microsoft-IIS/10.0<br />
X-AspNet-Version:4.0.30319<br />
X-Powered-By:ASP.NET<br />
X-Powered-By-Plesk:PleskWin<br />
Request Headers<br />
view source<br />
Accept:*/*<br />
Accept-Encoding:gzip, deflate<br />
Accept-Language:ar,en-US;q=0.9,en;q=0.8<br />
Connection:keep-alive<br />
Content-Length:237<br />
Content-Type:application/json; charset=UTF-8<br />
Host:asdm.sa<br />
Origin:http://asdm.sa<br />
Referer:http://asdm.sa/Sent.aspx<br />
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36<br />
X-Requested-With:XMLHttpRequest<br />
Request Payload<br />
view source<br />
{,…}<br />
resultData<br />
:<br />
"{"results": [{"msgId": "001","to": "9665312114","status": "D"}, {"msgId": "859911880","to": "966535112578","status": "N"}, {"msgId": "859911880","to": "966535112579","status": "S"}]}"


But when i post http://exam.medu.sa/sent.aspx not return OK
This code from F12 :
Request URL:http://asdm.sa/DLR.asmx/SaveData<br />
Request Method:OPTIONS<br />
Status Code:200 OK<br />
Remote Address:67.209.123.6:80<br />
Referrer Policy:no-referrer-when-downgrade<br />
Response Headers<br />
view source<br />
Allow:OPTIONS, TRACE, GET, HEAD, POST<br />
Content-Length:0<br />
Date:Mon, 29 Jan 2018 16:10:24 GMT<br />
Public:OPTIONS, TRACE, GET, HEAD, POST<br />
Server:Microsoft-IIS/10.0<br />
X-Powered-By:ASP.NET<br />
X-Powered-By-Plesk:PleskWin<br />
Request Headers<br />
view source<br />
Accept:*/*<br />
Accept-Encoding:gzip, deflate<br />
Accept-Language:ar,en-US;q=0.9,en;q=0.8<br />
Access-Control-Request-Headers:content-type<br />
Access-Control-Request-Method:POST<br />
Connection:keep-alive<br />
Host:asdm.sa<br />
Origin:http://localhost:59707<br />
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
 
Share this answer
 
Comments
Richard Deeming 30-Jan-18 11:32am    
If you want to update your question, click the green "Improve question" link and edit your question.

DO NOT post your update as a "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900