2011-05-14 231 views
0

我已经创建了类型post.When我从jquery ajax发送数据的服务不起作用。类型GET的方法正常工作。 我需要与职位类型也是。什么是解决方案。请帮助。jquery跨域问题!

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)); 
      } 
     }); 

问候, 吉瑞普山

+0

你为什么不这里发表您的代码示例。 – check123 2011-05-14 06:39:24

+0

@GIRI,发布您的代码... – kobe 2011-05-14 06:41:54

+0

请检查现在 – user601367 2011-05-14 06:49:56

回答

3

跨域请求使用JSONP解决。这实际上是一种破解浏览器安全模式的手段。它通过包含远程脚本块并在准备就绪时自动执行回调函数来工作。这只能做一个GET请求

0

一些提示:

    当u [R使用jQuery的 然后没必要弄典型 的JavaScript样式值
  • JSON.stringyfy()是没有必要的
  • 不使用数据作为变量名,误导(我认为数据被保留关键字为JS)

载荷以JSON块使用JSONP。将添加额外的“?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

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>