我已经创建了post类型的服务。当我从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));
}
});
此致 giri Bhushan
答案 0 :(得分:3)
使用JSONP解决跨域请求。这实际上是解决浏览器安全模型的黑客攻击。它的工作原理是包括一个远程脚本块,并在准备就绪时自动执行回调函数。这只能作为GET请求
完成答案 1 :(得分:0)
一些提示:
使用JSONP加载JSON块。会添加一个额外的“?callback =?”到URL的末尾以指定回调。
这里我修改了你的代码
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));
}
});
<强> REFERENCE 强>
答案 2 :(得分:0)
在您正在调用的服务方法中,在下面的函数的开头插入这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>