简体   繁体   English

无法为JavaScript中的Ajax调用加载资源错误

[英]Failed to load resource error for ajax call in javascript

I know there are many such questions, i tried those answers but my problem is not solved. 我知道有很多这样的问题,我尝试过这些答案,但是我的问题没有解决。

Am sending string to the webservice. 正在将字符串发送到Web服务。 But, values are not inserted in tables on server, whereas if I run the webservice in browser, it inserts the values. 但是,值不会插入服务器上的表中,而如果我在浏览器中运行Web服务,则会插入值。 So where is the problem exactly? 那么问题到底出在哪里呢?

https://coderwall.com/p/5nccwq https://coderwall.com/p/5nccwq

Failed to load resource under Chrome 无法在Chrome下加载资源

I referred the second link and tried to change according to the first answer in it, but doesn't work. 我引用了第二个链接,并尝试根据其中的第一个答案进行更改,但不起作用。

I tried to execute it in incognito mode, but no use. 我试图以隐身模式执行它,但没有用。

Please help with your answers. 请帮助您的答案。

var targeturl="http://hostname/projfolder/webservice.php?orderList="+str_table;
        console.log(targeturl);
$.ajax({
        type:"POST",
        url:targeturl,
        contentType: "application/json; charset=utf-8",
        crossDomain:true,
        dataType:'jsonp',
        success:function (data)
           {
            alert("in success");
            var parsedata=JSON.parse(JSON.stringify(data));
            alert("parsedata: "+parsedata);
            var stats=parsedata["Status"];
            alert("logindata: "+stats);

            if("1"==stats)
            {   
                alert("success");
            }
            else
            {
                alert("failed");
            }
          }
});

Actually I had forgot adding http:// , but I did it now, now here I get Unexpected token : error 其实我忘了添加http:// ,但是我现在就这样做了,现在在这里我得到了Unexpected token : error

I used many web services for the same project but did not go through such issue. 我在同一项目中使用了许多Web服务,但没有遇到此类问题。

Your server should understand and respond in JSONP. 您的服务器应理解并以JSONP响应。 It cannot simply return JSON. 它不能简单地返回JSON。

Example: 例:

  • suppose server needs to return json like {status:"1", msg:"success"} 假设服务器需要返回json,例如{status:"1", msg:"success"}
  • the response from the server will be like callback({status:"1", msg:"success"}) 来自服务器的响应将类似于callback({status:"1", msg:"success"})
  • The callback is random string that is automatically generated by JQuery and set in the query string of the request URL with parameter named callback . callback是由JQuery自动生成的random string ,并使用名为callback参数在请求URL的查询字符串中设置。
  • Your server should read this parameter from query string and then format the response as described in 2nd point above. 您的服务器应从查询字符串中读取此参数,然后按照上面第二点所述格式化响应。

Please read: 请阅读:

http://api.jquery.com/jquery.getjson/ http://api.jquery.com/jquery.getjson/

http://en.wikipedia.org/wiki/JSONP http://en.wikipedia.org/wiki/JSONP

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

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