0

我正在使用 WCF 服务从我的 jQueryMobile 应用程序获取数据并将数据发布到数据库。请注意我的服务在另一台服务器上。我能够从远程服务获取数据。但是我在使用 POST 方法更新数据时遇到问题。以下是我的代码,请帮助我找到解决方案。

<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title></title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
    <div data-role="page" id="index">
        <div data-role="content">
            <input name="text-1" id="text1" value="" type="text">
            <input name="text-2" id="text2" value="" type="text">
            <a href="#" data-role="button" id="useJSONP">Update</a>
        </div>
        <script type="text/javascript" charset="utf-8">
            $(document).on('pagebeforeshow', '#index', function () {
                $("#useJSONP").click(function () {
                    var id = $("#text1").val();
                    var name = $("#text2").val();
                    var userData = { "EmployeeID": id, "FirstName": name };
                    $.ajax({
                        url: "http://Mysite:83/FromDBDataService.svc/PostEmployeeData?callback=?",
                        type: "POST",
                        data: userData,
                        crossDomain: true,
                        contentType: "application/json; charset=utf-8",
                        dataType: "jsonp",
                        processdata: true,
                        success: function res(msg) {
                            alert("hello" + msg);
                        },
                        error: function error(response) {
                            alert("error");
                        }
                    });
                });
            });
        </script>
    </div>
</body>
</html>

WCF 服务:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;
using System.Reflection;

namespace ServiceSite
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class FromDBDataService 
    {
        [OperationContract]
        [WebInvoke(Method = "POST",
            BodyStyle = WebMessageBodyStyle.Wrapped,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        public void PostEmployeeData(int EmployeeID,string FirstName)
        {
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("update Employees set FirstName=@FirstName where EmployeeID=@EmployeeID", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.Parameters.Add(new SqlParameter("@EmployeeID", EmployeeID));
            cmd.Parameters.Add(new SqlParameter("@FirstName", FirstName));
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
   }
    public class EmployeeNew
    {
        public int EmployeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

谢谢,巴维亚。

4

1 回答 1

0

您将无法发出跨域 POST 请求。您可能会对一些技巧感兴趣。这里有另一个链接

我还建议您了解有关同源政策的更多信息。

JSONP 请求是通过放置<script></script>POST 请求无法执行的脚本来执行的。

于 2013-03-04T09:27:53.527 回答