0

我正在构建 WebApi - 已经在本地进行了测试,一切正常。部署到我的主机上,在确保 ajax 调用使用正确的 url 访问 webapi 的几个问题之后,我遇到了一个问题。

这是我的 Razor 代码,它显示了 jQuery AJAX 调用:

    string myUrl = Url.Action("Index","Home", null, Request.Url.Scheme, null);


<script type="text/javascript">
    function findByVrm() {
        $('#response').text('Searching...');

        var uri = '@myUrl' + 'api/vehicle';
        var id = $('#vrm').val();
        var call = null;

        // Setup a 'call' object differently depending upon if we want to return
        // JSON data or XML data
        if ($('#rbJson').prop('checked')) {
            call = {
                type: 'GET',
                url: uri + '?vrm=' + id
            };
        } else {
            call = {
                type: 'GET',
                url: uri + '?vrm=' + id,
                dataType: 'xml',
                accepts: {
                    xml: 'text/xml',
                    text: 'text/plain'
                }
            };
        }

        // Perform the API call
        $.ajax(call)
            .success(function (response, textStatus, jqXHR) {
                showStatus(jqXHR);

                if ($('#rbJson').prop('checked')) {
                    $('#response').html(vkbeautify.json(response));
                    $('#status').text('Vehicle Found: '+response.data.idscode);
                } else {
                    $('#response').text(vkbeautify.xml(jqXHR.responseText));  // text version of the XML
                }


            })
            .fail(function (jqXHR, textStatus, err) {
                showStatus(jqXHR);
                $('#response').text('');
            });
    }

    function showStatus(jqXHR) {
        var msg = 'Status: ' + jqXHR.status + '/' + jqXHR.statusText;
        $('#status').text(msg);

        return;
    }          
</script>

HTML 纯粹是一个 id 为“vrm”的文本框,这是搜索字符串,一个调用 findByVrm() javascript 函数的按钮,一个指示调用是否应返回 JSON 或 XML 数据的单选按钮,一个要显示的文本框通话状态,以及显示内容的文本区域。

在本地主机上运行时,这一切都完美无缺。当部署到远程主机,并在 Fiddler 中构建调用时,它可以完美运行。

但是,当在远程主机上浏览此页面并通过上述代码进行调用时...调用 API 方法(我有显示它被调用的日志消息,并且发送回了响应),状态为更新并显示 200/OK - 向我表明调用成功,但是在此实例中似乎未返回 JSON 或 XML 数据(或者如果已返回,则不可见)。

在 stackoverflow 和其他网站上对此进行了研究后,我认为这是一个“跨域”问题,并且已经关注了其他有关实现 CORS 处理程序的帖子——然而,当 webapi 托管在远程服务器上并且上面的 Javascript 被调用时,我仍然没有看到响应数据。

我从这里获取了 CORS 处理程序代码:CORS Handler

有任何想法吗?


EDIT: Further to the post above and the questions asked below - I've created a JSFiddle to see if I could replicate the issue that I was seeing in isolation - Here is the Fiddle

The Fiddle WORKS -- however the above Javascript isn't working - so my initial conclusion here is the vkbeautify is failing, and consequentially quietly exiting the findByVrm() method.

I will perform a bit more testing on this; but I'd like to thank everyone who's spent time looking at this with me.


EDIT: So by removing the vkbeautify.json() call and replacing with a JSON.stringify() and placing the result of that into the textarea -- this works.

I'm not sure why vkbeautify worked on response data when I was running it in localhost through Visual Studio, but it's irrelevant -- it'd be nice to be able to 'pretty print' both the JSON and XML responses - but there are other solutions I can look into for that; this is just a test-bed application, so as long as I can see the response, that's all I need.

Again thank you for all your help.

4

1 回答 1

0

As mentioned above, the issue was with vkbeautify - for some reason it wasn't working with the remote data; but was working fine when running locally.

I've now removed the calls to this javascript library, it was only there to pretty-print the JSON or XML returned data.

于 2013-10-30T10:29:51.100 回答