Ajax调用没有达到控制器/动作中的url路径 [英] Ajax call does not hit the url path in controller/action

查看:261
本文介绍了Ajax调用没有达到控制器/动作中的url路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于下拉列表的OnChange事件,我正在进行ajax调用。在url路径中,当我给出Controller / Action路径时,它根本不会触及该方法。

我的代码如下:



  function  FillHO(){
// alert('Test');
var stateId = $(' #ddlHO')。val();
$ .ajax({
type: POST
url:' @ Url.Action(SetSelectedHO,.. /../ Controllers / ReportsController)'
contentType: application / json; charset = utf-8
dataType: json
data:' {SelectedValue:' + stateId + ' }'
成功: function (响应){
alert(' 测试');
},
失败:功能(响应){
alert(response.d);
}
});
}





我的尝试:



我试过'../../Controllers/ReportsController/SetSelectedHO'

'ReportsController / SetSelectedHO'

解决方案

< blockquote>(' #ddlHO')。val();


< blockquote> .ajax({
type: POST
url: ' @ Url.Action(SetSelectedHO,.. /../ Controllers / ReportsController)'
contentType: application / json; charset = utf-8
dataType: json
data:' {SelectedValue:' + stateId + ' }',
成功: function (响应){
alert(' 测试');
},
失败: function (响应){
alert(response.d);
}
});
}





我的尝试:



我试过'../../Controllers/ReportsController/SetSelectedHO'

'ReportsController / SetSelectedHO'


使用开发工具( Chrome中的F12)用于检查客户端具有的JavaScript。您可能会发现为该呼叫提供的URL不是您所期望的。



这是......



 url:'  @ Url.Action(SetSelectedHO, ../../ Controllers / ReportsController)' 





...生成有效的URL?看起来好像是在设置文字,而不是@ URL.Action方法的返回值。


For OnChange event of a dropdown, I am making a ajax call. In the url path, when I give the Controller/Action path it does not hit the method at all.
My code looks like:

function FillHO() {
    //alert('Test');
    var stateId = $('#ddlHO').val();
    $.ajax({
        type: "POST",
        url: '@Url.Action("SetSelectedHO","../../Controllers/ReportsController")' 
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: '{SelectedValue: "' + stateId + '" }',
        success: function (response) {
            alert('Test');
        },
        failure: function (response) {
        alert(response.d);
    }
    });
}



What I have tried:

I have tried '../../Controllers/ReportsController/SetSelectedHO'
'ReportsController/SetSelectedHO'

解决方案

('#ddlHO').val();


.ajax({ type: "POST", url: '@Url.Action("SetSelectedHO","../../Controllers/ReportsController")' contentType: "application/json; charset=utf-8", dataType: "json", data: '{SelectedValue: "' + stateId + '" }', success: function (response) { alert('Test'); }, failure: function (response) { alert(response.d); } }); }



What I have tried:

I have tried '../../Controllers/ReportsController/SetSelectedHO'
'ReportsController/SetSelectedHO'


Use dev tools (F12 in Chrome) to inspect the JavaScript that the client has. You might find the URL supplied for the call is not what you expect it to be.

Does this...

url: '@Url.Action("SetSelectedHO","../../Controllers/ReportsController")'



... generate a valid URL? It looks as though you're setting a literal and not the return value of the @URL.Action method.


这篇关于Ajax调用没有达到控制器/动作中的url路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆