简体   繁体   English

jQuery Ajax在HTTPS上的调用

[英]jQuery Ajax call on HTTPS

I have a form that takes three steps to fill it. 我有一个表格,需要三个步骤来填写。 At each of these steps you fill some info, click "Next" and then see either errors you have to fix in order to get to the next step or if there are no errors it takes you to the next step (change "display" to "block" in CSS). 在上述每个步骤中,您都填写了一些信息,请单击“下一步”,然后查看您必须修复的错误才能进入下一步,或者如果没有错误,则将您带到下一步(将“显示”更改为CSS中的“ block”)。

At first step it uses $.ajax() to call file lib/ajax.php which returns some results in JSON that matter a lot to filling the rest of the form. 第一步,它使用$ .ajax()调用文件lib / ajax.php ,该文件在JSON中返回一些结果,这对填充表单的其余部分非常重要。 The thing is this works locally on my http://my.dev address, but live on https://something.example.com it does not. 问题是这可以在我的http://my.dev地址上本地运行,但不能在https://something.example.com上运行。

Where should I first try looking to go about it? 我首先应该在哪里寻找解决方案? What are the most common workarounds on this? 最常见的解决方法是什么?

Both lib/ajax.php and the index.php file (that has the jQuery call) are on the same domain ( https://something.example.com ) so we are not talking about cross-domain calls. lib / ajax.phpindex.php文件(具有jQuery调用)都在同一个域( https://something.example.com )上,因此我们不在谈论跨域调用。

Here's some of the code. 这是一些代码。

$.ajax({
   type: 'GET',
   url: 'lib/ajax.php', 
   async: false,
   data: {id: id}, 
   dataType: 'json', // what type of data do we expect back from the server
   }).done(function(data){
       // do stuff with data
});

and the lib/ajax.php lib / ajax.php

//stuff with db and retrieving row by id
header('Content-type: application/json');

echo json_encode($result);

That's pretty much it. 就是这样。 It doesn't work when I place it on live HTTPS server. 当我将其放置在实时HTTPS服务器上时,它不起作用。

在https中,我认为您必须将dataType: 'json'更改为dataType: 'jsonp'

1.Check the whether the url given in the function $.ajax() is the right path or not. 1.检查函数$ .ajax()中给出的url是否正确。

2.Here you specified data type as "JSON",so response must be "JSON". 2.此处将数据类型指定为“ JSON”,因此响应必须为“ JSON”。

3.If the response is not in correct JSON format,it makes issue. 3.如果响应的格式不正确,则会出现问题。

  contentType: "application/json; charset=utf-8"

Add the above code in $.ajax() function 在$ .ajax()函数中添加以上代码

Please check the above specified points 请检查以上指定点

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

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