我创建了一个帖子类型的服务。当我从jquery ajax发送数据时,它不起作用。GET类型的方法工作正常。我还需要帖子类型,解决方案是什么。请帮忙。
var user = document.getElementById("uname").value;
var pass = document.getElementById("pass").value;
var utyp = document.getElementById("usertyp").value;
// alert("hi"+pass);
var dat = { "uname": user, "pwd": pass, "utype": utyp };
Data = JSON.stringify(dat);
$.ajax({
url: "http://192.168.1.9:450/Service.svc/Login_Insert",
type: "POST",
data:Data,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processdata: true,
success: function res(msg) {
alert("hello" + msg);
},
error: function error(response) {
alert("error");
alert(response.responseText);
alert(JSON.stringify(response));
}
});
问候吉里·布山
跨域请求使用JSONP解决。这实际上是绕过浏览器安全模型的黑客攻击。它的工作原理是包含一个远程脚本块,并在准备就绪时自动执行回调函数。这只能作为 GET 请求完成
一些提示:
- 当你使用jQuery时,不需要用典型的JavaScript风格获取值。
- JSON.stringyfy() 不是必需的
- 不要使用DATA作为变量名,误导(我认为数据是js的保留关键字)
使用 JSONP 在 JSON 块中加载。将在 URL 末尾添加一个额外的"?callback=?" 以指定回调。
在这里我修改了你的代码
var user = $("#uname").val();
var pass = $("#pass").val();
var utyp = $("#usertyp").val();
var userData = { "uname": user, "pwd": pass, "utype": utyp };
$.ajax({
url: "http://192.168.1.9:450/Service.svc/Login_Insert?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");
alert(response.responseText);
alert(JSON.stringify(response));
}
});
参考
在您调用的服务方法中,在下面的函数开头插入这 4 行代码。
public string GetEmployee(string id, string name, string email)
{
WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Access-Control-Allow-Origin", "*"); WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Access-Control-Allow-Methods", "GET"); WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Access-Control-Allow-Headers", "Content-Type, Accept");
Employee employee = new Employee();
employee.TRGEmpID = id;
employee.First_Name = name;
employee.Email = email;
EmployeeManagement empMgmt = new EmployeeManagement();
var json = new JavaScriptSerializer().Serialize(empMgmt.Search(employee).FirstOrDefault());
return json;
}
此外,在 SVC 文件中,在函数之前添加此行,如下所示。
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
string GetEmployees(string name);
如果你也需要web.config,这里是
<?xml version="1.0"?>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="EmployeeService.EmployeeSearchService" behaviorConfiguration="DefaultServiceBehavior">
<endpoint binding="webHttpBinding" contract="EmployeeService.IEmployeeSearchService" behaviorConfiguration="DefaultEPBehavior" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="DefaultEPBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="DefaultServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>