数据格式 Jquery Ajax 发布跨域调用 Asp.Net MVC 方法



我正在尝试从http到https跨域的MVC调用后的$.ajax。

客户端

enter code here
$.ajax({    
type: 'post',    
crossDomain: true,    
url: 'https://localhost/views/Member/VerifyEmail',    
beforeSend: function () { alert('I am sending'); },    
data: '{name:"John"}',    
dataType: "json",    
success: function (data) { pdata = data; }    
});

服务器端

 [RequireHttps]    
 [HttpPost]    
 public string VerifyEmail(string name){    
   return "got it"
 }

我已将访问控制允许源添加到web.config以便可以正常建立调用。现在问题出在服务器端,我得到了变量名 = null

我还检查了调试,发现数据实际上已发送到服务器

HttpContext.Request.Form    
{%7bname%3a%22hello%22%7d}    
[System.Web.HttpValueCollection]: {%7bname%3a%22hello%22%7d}    

问题是我如何从 Web 方法中检索它?

%7bname%3a%22hello%22%7d 这是 HTML 实体字符串请解码字符串然后解析为 JSON。

我认为您可以将呼叫更改为

$.ajax({    
 type: 'post',    
 crossDomain: true,    
 url: 'https://localhost/views/Member/VerifyEmail',    
 beforeSend: function () { alert('I am sending'); },    
 data: 'John',    
 dataType: "text",    
 success: function (data) { pdata = data; }    
}); 

最新更新