Web服务仅在从本地调用时才起作用 [英] Web service work only when call from local

查看:68
本文介绍了Web服务仅在从本地调用时才起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





网络服务工作当来自其他主机的本地非工作电话时



i创建Web服务以添加数据(从其他服务器发送)到我的数据



代码Web服务

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

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 :

<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>





网站1中的网络服务



当我从相同的服务器发布json :http://website1.com/sent.aspx 工作



但是当我从其他服务器发布json,例如:اttp://website2.com/sent.aspx 不工作



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



我尝试过:



我解决了它我把这个代码添加到web.config



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

<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>

推荐答案

.ajax({
type: POST
url: < span class =code-string> http://website1.com/DLR.asmx/SaveData,
contentType: < span class =code-string> application / json; charset = utf-8,
data: JSON .stringify({resultData :json}), // 将json字符串传递给Web服务
传统: true
processData: false
成功: function (data){}

});
< / script>
.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>





网站1中的网络服务



当我从相同的服务器发布json :http://website1.com/sent.aspx 工作



但是当我从其他服务器发布json,例如:اttp://website2.com/sent.aspx 不工作



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



我尝试过:



我解决了它我把这个代码添加到web.config



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

<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>


您的问题是您尝试从域A向域上的Web服务发出请求B通过客户端javascript。您遇到的问题是跨域资源共享阻止您对域A上的Web服务的请求。



基本上,如果你能够,你需要为您的Web服务启用CORS请求。



我觉得你有两个选择



*我不知道这是不是web api但是您需要弄清楚如何为您的网络服务启用cors请求。

Google [ ^ ]



启用asp.net中的cors mvc - Google搜索 [ ^ ]



Google [ ^ ]



这3个谷歌搜索有大量的相关链接,你需要自己做腿部工作来弄清楚会发生什么为你的项目工作。



*另一个选择是你在项目中添加一个服务器端方法/操作,然后调整你的
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 在客户端调用来调用,并从新添加的服务器端代码中调用域A的Web服务,绕过CORS问题。
.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.


这篇关于Web服务仅在从本地调用时才起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆