简体   繁体   English

Ajax在产品服务器上不再工作

[英]Ajax doesn't work anymore on the prod server

I'm a web developper in a small company that decided to make its selling website in PHP5/Laravel 4.2 instead of Prestashop or anything else. 我是一家小型公司的网络开发人员,因此决定使用PHP5 / Laravel 4.2而不是Prestashop或其他任何工具来创建其销售网站。 The site is hosted by another company, and we don't have the liberty to make anything we'd want on the server. 该站点由另一家公司托管,我们没有自由在服务器上进行任何操作。

I made 6 monthes ago a script in AJAX that let the user know without refreshing how high would his shipping fee and taxes cost. 我在6个月前用AJAX编写了一个脚本,该脚本让用户知道而又无需刷新其运费和税金。 We're in Europe in the border of France and Switzerland, so it's really a necessity to have this script. 我们在欧洲的法国和瑞士边境,因此确实有必要编写此脚本。 It worked really great, until this week when I wanted to implement another script, and found out that this script didn't work anymore (for how long, I don't know). 直到本周我想实现另一个脚本,并发现该脚本不再起作用(我已经知道了多长时间)为止,它的运行效果非常好。

The JS : JS:

function shippingFee()
{
        var idCountry, weight;
        weight= document.getElementById('weight').value;
        idCountry = document.getElementById("country").value;
        $.post('ajax/shippingFee',               
            {idCountry: idCountry, weight: weight},          
            function(data){
                obj = JSON.parse(data);
            $('#fee).html(obj.fee); 
            $('#total').html(obj.total);  
            $('#sub').html(obj.sub);  
            $('#promo').html(obj.promo);         
            });                     
};

Here is my route : 这是我的路线:

Route::post('ajax/shippingFee', 'AjaxController@shippingFee');

Here's my function (not everything, just the final result) on the AjaxController: 这是我在AjaxController上的功能(不是全部,只是最终结果):

public function shippingFee(){

//Test values, only for this example
    $fee = 12;
    $total = 24;
    $sub = 3;
    $promo = 0;

    $res = ['fee'=>$fee, 'total'=>$total, 'sub'=>$sub, 'promo'=>$promo];
    echo json_encode($res); 
}

When I click on the button that activate the function, I have this error : 当我单击激活功能的按钮时,出现此错误:

SyntaxError : unexpected token in json at position 0

And if I click on the source, I have my JSON : 如果单击源,我将得到JSON:

{"fee": 12, "total":24, "sub":3, "promo":0}

Again, the code worked perfectly six monthes ago, and still works on localhost. 同样,该代码在六个月前运行完美,并且仍然可以在localhost上运行。 Do you have any idea on what's happening ? 您对发生的事情有任何想法吗?

Thanks a lot. 非常感谢。

specify the dataType in Post request, also check the response result in Developer tools that will give you clear idea. 在发布请求中指定dataType,并在开发人员工具中检查响应结果,这将为您提供清晰的思路。 don't forget to call exit(0) method once you echo result that will fix the issue if you using any framework 一旦回显结果,别忘了调用exit(0)方法,如果您使用任何框架,它将解决此问题

PHP Exit method PHP退出方法

Okay, I succeeded. 好的,我成功了。 I don't know why, but my json_encode wasn't sending a satisfying json. 我不知道为什么,但是我的json_encode没有发送令人满意的json。 I tried to change the structure of my function with this : 我试图用以下方法更改函数的结构:

function shippingFee()
{
        var idCountry, weight;
        weight= document.getElementById('weight').value;
        idCountry = document.getElementById("country").value;

            $.ajax({
            url: "ajax/shippingFee",
            type: "Post",
            data: {'idCountry':idCountry, 'weight':weight},
            traditional: true,
            success: function (data) {

                data = JSON.stringify(eval('('+data+')'));
                var obj= JSON.parse(data);
                $('#fee).html(obj.fee); 
                $('#total').html(obj.total);  
                $('#sub').html(obj.sub);  
                $('#promo').html(obj.promo); 
                }
            });                     
};

Thus, with the data = JSON.stringify(eval('('+data+')')); 因此,使用data = JSON.stringify(eval('('+data+')')); I say that this is a JSON. 我说这是JSON。 And my code is working like a charm. 而且我的代码像一个魅力一样工作。 I found this solution by watching this post : Convert object string to JSON 我通过看这篇文章找到了这个解决方案: 将对象字符串转换为JSON

I tried to put the dataType:"JSON" before the success, but when I did it returned an error. 我试图将dataType:"JSON"放在成功之前,但是当我这样做时,它返回了一个错误。 With contentType:"application/json" I had an error 500. 使用contentType:"application/json"时出现错误500。

Thanks to the people who answered me :) 感谢回答我的人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM